如何在同一个测试中使用 TestCaseSource 和 TestOf 属性?
How to use TestCaseSource and TestOf attributes in the same test?
我正在尝试按如下方式定义 parameterized test that receives data using the TestCaseSource attribute. I also need to define an attribute to relate each test with a Jira ticket, and as recommended , I'm using the TestOf 属性:
[TestOf("SomeId")]
[TestCaseSource(typeof(SomeProviderClass), "someMethod")]
public void SampleTest(dynamic myData)
{
//do something with myData
//assert something
}
然而,当执行测试时,在 TearDown 中,应该具有 TestOf 值的 TestContext.CurrentContext.Test.Properties 的大小为空。
我应该如何定义属性才能被 NUnit 正确识别?
当您使用 TestCaseSource 时,您实际上是在创建一个 'suite' 测试。 NUnit 测试以树的形式构建。通常,您的程序集将是树的顶层根,并且此分支中的每个 class 依次分支以包括所有单独的测试方法。即
-Test1
- TestClassA -Test2
-Test3
TestAssembly
-Test4
- TestClassB -Test5
-Test6
如果你看一下 TestResults.xml 就可以看到这个结构。
当您使用 TestCaseSource
时,您实际上是在树上创建另一个级别。所以现在你的树看起来像这样。
-Test1A
-TestCaseSourceSuite -Test1B
-Test1C
- TestClassA -Test2
-Test3
TestAssembly
-Test4
- TestClassB -Test5
-Test6
在您的示例中,TestCaseSourceSuite
将被命名为 SampleTest
。您当前拥有的内容不起作用的原因是 TestOf
属性当前应用于 TestCaseSourceSuite
,而不是 Test1A
、Test1B
Test1C
。 (像 TestOf 这样的属性不会按层次结构向上或向下复制树。)
无论如何。我们如何解决它?将 TestCaseData 用于 TestCaseSource 似乎是最好的选择。 TestOfAttribute 实际上并没有多大用处,因此没有用于设置值的 'named property',但您可以使用 SetProperty
方法和 PropertyNames
class 代替。试试这样的东西...
public class MyTests
{
[TestCaseSource(typeof(MyDataClass), "TestCases")]
public void Test(int n, int d)
{
Assert.IsTrue(true);
}
}
public class MyDataClass
{
public static IEnumerable TestCases
{
get
{
yield return new TestCaseData(12, 3).SetProperty(PropertyNames.TestOf, "MethodUnderTest");
yield return new TestCaseData(12, 2).SetProperty(PropertyNames.TestOf, "MethodUnderTest");
}
}
}
希望对您有所帮助!
我正在尝试按如下方式定义 parameterized test that receives data using the TestCaseSource attribute. I also need to define an attribute to relate each test with a Jira ticket, and as recommended
[TestOf("SomeId")]
[TestCaseSource(typeof(SomeProviderClass), "someMethod")]
public void SampleTest(dynamic myData)
{
//do something with myData
//assert something
}
然而,当执行测试时,在 TearDown 中,应该具有 TestOf 值的 TestContext.CurrentContext.Test.Properties 的大小为空。 我应该如何定义属性才能被 NUnit 正确识别?
当您使用 TestCaseSource 时,您实际上是在创建一个 'suite' 测试。 NUnit 测试以树的形式构建。通常,您的程序集将是树的顶层根,并且此分支中的每个 class 依次分支以包括所有单独的测试方法。即
-Test1
- TestClassA -Test2
-Test3
TestAssembly
-Test4
- TestClassB -Test5
-Test6
如果你看一下 TestResults.xml 就可以看到这个结构。
当您使用 TestCaseSource
时,您实际上是在树上创建另一个级别。所以现在你的树看起来像这样。
-Test1A
-TestCaseSourceSuite -Test1B
-Test1C
- TestClassA -Test2
-Test3
TestAssembly
-Test4
- TestClassB -Test5
-Test6
在您的示例中,TestCaseSourceSuite
将被命名为 SampleTest
。您当前拥有的内容不起作用的原因是 TestOf
属性当前应用于 TestCaseSourceSuite
,而不是 Test1A
、Test1B
Test1C
。 (像 TestOf 这样的属性不会按层次结构向上或向下复制树。)
无论如何。我们如何解决它?将 TestCaseData 用于 TestCaseSource 似乎是最好的选择。 TestOfAttribute 实际上并没有多大用处,因此没有用于设置值的 'named property',但您可以使用 SetProperty
方法和 PropertyNames
class 代替。试试这样的东西...
public class MyTests
{
[TestCaseSource(typeof(MyDataClass), "TestCases")]
public void Test(int n, int d)
{
Assert.IsTrue(true);
}
}
public class MyDataClass
{
public static IEnumerable TestCases
{
get
{
yield return new TestCaseData(12, 3).SetProperty(PropertyNames.TestOf, "MethodUnderTest");
yield return new TestCaseData(12, 2).SetProperty(PropertyNames.TestOf, "MethodUnderTest");
}
}
}
希望对您有所帮助!