nick gowdy
nick gowdy

Reputation: 6531

nunit - use multiple TestFixtureSource on class

I'm trying to do something with nunit but I'm not sure if it's possible. Basically I am trying to reduce my code setup by using TestFixtureSource. I've written this:

SomeTestClass.cs

[TestFixtureSource(typeof(FixtureData1), "FixtureParams")]
[TestFixtureSource(typeof(FixtureData2), "FixtureParams")]
    public class SomeTestClass: BaseRepositoryTests
    {
        private readonly Foo _foo;
        private readonly Bar _bar;

        public SomeTestClass(Foo foo, Bar bar) 
        {
            _foo = foo;
            _bar = bar;
        }

        [Test]
        public async Task SomeTest()
        {

        }
    }

Foo.cs

public class Foo
    {
        public static IEnumerable<Foo> FixtureParams
        {
            get
            {
                yield return new Foo
                {
                    FooId = 0,
                    FooName= "meh",
                };
            }
        }
    }

Bar.cs

 public class Bar
    {
        public static IEnumerable<Bar> FixtureData
        {
            get
            {
                yield return new Bar
                    {Email = "[email protected]", FirstName = "test", Surname = "user"};
            }
        }
    }

I get this error:

Message: OneTimeSetUp: No suitable constructor was found

Anyone know if this is possible in nunit?

Upvotes: 0

Views: 550

Answers (2)

Charlie
Charlie

Reputation: 13736

OTOH if you want NUnit to take values for foo and bar and combine them in various ways... you should ask that question. :-)

Upvotes: 0

Nkosi
Nkosi

Reputation: 247521

According to documentation It does not appear you can do what it is you are trying to do

Here is one example

[TestFixtureSource(typeof(FixtureArgs))]
public class SomeTestClass: BaseRepositoryTests {
    private readonly Foo _foo;
    private readonly Bar _bar;

    public SomeTestClass(Foo foo, Bar bar)  {
        _foo = foo;
        _bar = bar;
    }

    [Test]
    public async Task SomeTest() {
        //...
    }
}


class FixtureArgs: IEnumerable {
    public IEnumerator GetEnumerator() {
        yield return new object[] { 
            new Foo { FooId = 0, FooName= "meh" }, new Bar { Email = "[email protected]", FirstName = "test", Surname = "user"} 
        };

        yield return new object[] { 
            new Foo { FooId = 1, FooName= "meh" }, new Bar { Email = "[email protected]", FirstName = "test", Surname = "user"} 
        };

        //...
    }
}

Here is another

[TestFixtureSource(typeof(AnotherClass), "FixtureArgs")]
public class SomeTestClass: BaseRepositoryTests {
    private readonly Foo _foo;
    private readonly Bar _bar;

    public SomeTestClass(Foo foo, Bar bar)  {
        _foo = foo;
        _bar = bar;
    }

    [Test]
    public async Task SomeTest() {
        //...
    }
}

class AnotherClass
{
    static object [] FixtureArgs = {
        new object[] { new Foo { FooId = 0, FooName= "meh" }, new Bar { Email = "[email protected]", FirstName = "test", Surname = "user"}  },
        new object[] {  new Foo { FooId = 1, FooName= "meh" }, new Bar { Email = "[email protected]", FirstName = "test", Surname = "user"}  }
    };
}

Reference TestFixtureSource Attribute

Upvotes: 1

Related Questions