使用Nunit或Jenkins多次运行TestFixture

时间:2015-12-01 15:39:47

标签: c# jenkins nunit jenkins-plugins

我有一个包含多个测试的TestFixture。我想多次运行这个TestFixture来收集一些统计数据,但似乎找不到合理的方法。 RepeatAttribute不能用于TestFixture。

我可以使用Nunit或Jenkins(使用nunit插件)。有任何想法吗?否则我最终会将其编码到测试中或使用奇怪的批处理脚本。

3 个答案:

答案 0 :(得分:2)

正如史蒂夫所说,参数化的装置可以工作,但是如果你想要运行你的测试数百次,那么复制和粘贴那么多属性会相当繁琐。

NUnit中更好的选择是在灯具上使用TestFixtureSource属性。例如,以下代码将运行您的测试夹具100次;

[TestFixtureSource("TestData")]
public class MultipleRunFixture
{
    int _counter;

    public MultipleRunFixture(int counter)
    {
        _counter = counter;
    }

    public static IEnumerable<int> TestData =>
        Enumerable.Range(0, 100);

    [Test]
    public void TestMethod()
    {
        // TODO: Add your test code here
        Assert.Pass($"Test run {_counter}");
    }
}

答案 1 :(得分:0)

你可以把它变成一个典型的夹具:然后夹具会运行你添加属性的次数:

[TestFixture(4)]
[TestFixture(3)]
[TestFixture(2)]
[TestFixture(1)]
public class MyTestFixture
{       
    public MyTestFixture(int counter)
    {        
    }

答案 2 :(得分:0)

感谢Steve和Rob的回答。所有这些都是不错的选择,但我尝试了另一种方法,结果证明这是我案例的最佳解决方案。所以只要把它放在任何偶然发现它的人身上。

我为该测试夹具创建了一个Jenkins工作,并使用cron时间定期重复测试,比如每2分钟一次。这样,詹金斯多次完成这项工作,甚至为我收集了统计数据(例如测试失败率)。

相关问题