C# nunit-在类上使用多个TestFixtureSource

C# nunit-在类上使用多个TestFixtureSource,c#,nunit,C#,Nunit,我正试图和努伊特做点什么,但我不确定这是否可能。基本上,我试图通过使用TestFixtureSource来减少我的代码设置。我写了这样一篇文章: SomeTestClass.cs Foo.cs 有人知道这在nunit中是否可行吗?根据文档,似乎你无法做到你想做的事情 这里有一个例子 [TestFixtureSource(typeof(FixtureArgs))] public class SomeTestClass: BaseRepositoryTests { private reado

我正试图和努伊特做点什么,但我不确定这是否可能。基本上,我试图通过使用TestFixtureSource来减少我的代码设置。我写了这样一篇文章:

SomeTestClass.cs

Foo.cs


有人知道这在nunit中是否可行吗?

根据文档,似乎你无法做到你想做的事情

这里有一个例子

[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 = "test.user@google.com", FirstName = "test", Surname = "user"} 
        };

        yield return new object[] { 
            new Foo { FooId = 1, FooName= "meh" }, new Bar { Email = "test.user@google.com", FirstName = "test", Surname = "user"} 
        };

        //...
    }
}
这是另一个

[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 = "test.user@google.com", FirstName = "test", Surname = "user"}  },
        new object[] {  new Foo { FooId = 1, FooName= "meh" }, new Bar { Email = "test.user@google.com", FirstName = "test", Surname = "user"}  }
    };
}

如果您希望NUnit获取foo和bar的值并以各种方式组合它们,请参考

oth。。。你应该问那个问题-

没错。与TestCaseSource类似,TestFixtureSource旨在一次提供所有必要的参数。如果您发现所有对象[]都令人困惑,请使用`TestCaseData作为嵌套对象。
 public class Bar
    {
        public static IEnumerable<Bar> FixtureData
        {
            get
            {
                yield return new Bar
                    {Email = "test.user@google.com", FirstName = "test", Surname = "user"};
            }
        }
    }
Message: OneTimeSetUp: No suitable constructor was found
[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 = "test.user@google.com", FirstName = "test", Surname = "user"} 
        };

        yield return new object[] { 
            new Foo { FooId = 1, FooName= "meh" }, new Bar { Email = "test.user@google.com", FirstName = "test", Surname = "user"} 
        };

        //...
    }
}
[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 = "test.user@google.com", FirstName = "test", Surname = "user"}  },
        new object[] {  new Foo { FooId = 1, FooName= "meh" }, new Bar { Email = "test.user@google.com", FirstName = "test", Surname = "user"}  }
    };
}