在xUnit.net测试参数类似NUnit的 [英] Test parameterization in xUnit.net similar to NUnit

查看:149
本文介绍了在xUnit.net测试参数类似NUnit的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有xUnit.net框架类似NUnit的以下功能的任何方式?



  [测试,TestCaseSource( CurrencySamples)] 
公共无效Format_Currency(十进制值,字符串预期){}

静态对象[] [] CurrencySamples =新对象[] []
{
新的对象[] {0米,0,00},
新的对象[] {0.0004米,0,00},
新的对象[] {5M,5,00 }
新的对象[] {5.1米,5,10},
新的对象[] {5.12米,5,12},
新的对象[] {5.1234男,5,12},
新的对象[] {5.1250米,5,13} //圆
新的对象[] {5.1299米,5,13}, //圆
}

这将在NUnit的GUI产生8个独立的测试

  [TestCase的((字符串)空,结果=1)] 
[TestCase的(,结果=1 )]
[测试用例(,结果=1)[
[测试用例(1,结果=2)]
[测试用例(1,结果= 2)]
公共字符串IncrementDocNumber(字符串lastNum){返回一些; }

这将产生5个独立的测试,并自动比较结果(Assert.Equal)。

  [测试] 
公共无效StateTest(
[值(1,10)]
INT输入,
[值(State.Initial,State.Rejected,State.Stopped)]
DocumentType DOCTYPE
){}

这将产生6的组合测试。无价的。



几年前我试过的xUnit和喜爱它,但它缺乏这些功能。生活不能没有他们。有某种改变?


解决方案

的xUnit 提供一种方式来运行的参数测试的通过一些所谓的数据理论。这个概念是等同于一个在NUnit的发现,但你得到的开箱功能的并不完整



下面是一个例子:

  [理论] 
[InlineData(富)]
[InlineData(9)]
[InlineData(真)
公共无效Should_be_assigned_different_values​​(对象的值)
{
Assert.NotNull(值);
}

在这个例子中的xUnit将运行 Should_format_the_currency_value_correctly 测试一次,每 InlineDataAttribute 每次路过指定的值作为参数。<​​/ p>

数据的理论是一个的扩展点的,你可以用它来创建新的方法来运行参数的测试。这样做的方式是通过的创建新的属性的检查和可选择在测试方法的参数和返回值的行为。



您可以找到如何的xUnit数据理论可以的 AutoFixture AutoData 并的 InlineAutoData 理论。


Are there any means in xUnit.net framework similar to the following features of NUnit?

[Test, TestCaseSource("CurrencySamples")]
public void Format_Currency(decimal value, string expected){}

static object[][] CurrencySamples = new object[][]
{
    new object[]{ 0m, "0,00"},
    new object[]{ 0.0004m, "0,00"},
    new object[]{ 5m, "5,00"},
    new object[]{ 5.1m, "5,10"},
    new object[]{ 5.12m, "5,12"},
    new object[]{ 5.1234m, "5,12"},
    new object[]{ 5.1250m, "5,13"}, // round
    new object[]{ 5.1299m, "5,13"}, // round
}

This will generate 8 separate tests in NUnit GUI

[TestCase((string)null, Result = "1")]
[TestCase("", Result = "1")]
[TestCase(" ", Result = "1")]
[TestCase("1", Result = "2")]
[TestCase(" 1 ", Result = "2")]
public string IncrementDocNumber(string lastNum) { return "some"; }

This will generate 5 separate tests and automatically compare the results (Assert.Equal).

[Test]
public void StateTest(
    [Values(1, 10)]
    int input,
    [Values(State.Initial, State.Rejected, State.Stopped)]
    DocumentType docType
){}

This will generate 6 combinatorial tests. Priceless.

Few years ago I tried xUnit and loved it but it lacked these features. Can't live without them. Has something changed?

解决方案

xUnit offers a way to run parameterized tests through something called data theories. The concept is equivalent to the one found in NUnit but the functionality you get out of the box is not as complete.

Here's an example:

[Theory]
[InlineData("Foo")]
[InlineData(9)]
[InlineData(true)]
public void Should_be_assigned_different_values(object value)
{
    Assert.NotNull(value);
}

In this example xUnit will run the Should_format_the_currency_value_correctly test once for every InlineDataAttribute each time passing the specified value as argument.

Data theories are an extensibility point that you can use to create new ways to run your parameterized tests. The way this is done is by creating new attributes that inspect and optionally act upon the arguments and return value of the test methods.

You can find a good practical example of how xUnit's data theories can be extended in AutoFixture's AutoData and InlineAutoData theories.

这篇关于在xUnit.net测试参数类似NUnit的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆