简体   繁体   English

如何定义单元测试的不同顺序?

[英]How can i define a different order of my unit tests?

I have these 3 tests:我有这 3 个测试:

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Threading;

namespace FirstTestCase
{
class _04_02_Media
{

    class NUnitTest
    {
[TestCase(TestName = "04_02_01_Libraries_Add_OnDemand_Video")]
                public void Libraries()
                {}
    [TestCase(TestName = "04_02_02_Replace_OnDemand")]
                public void OnDemandReplace()
                {}
    [TestCase(TestName = "04_02_03_Delete_OnDemand")]
                public void OnDemandDelete()
                {}

For some reason i cannot understand and is making me go crazy, the "delete" test, the one supposed to be the last, happens second.出于某种原因,我无法理解并且让我发疯,“删除”测试,应该是最后一个,发生在第二个。 This is a big deal as the "replace" test, that happens last, uses the deleted video.这是一个大问题,因为最后发生的“替换”测试使用已删除的视频。

Why does it run in this order?为什么按这个顺序运行? Is there anything else i should use to change the order?还有什么我应该用来改变订单的吗?

You can use the Order attribute to specify the order:您可以使用Order属性来指定顺序:

[Order(1)]
public void Test1() { /* ... */ }

[Order(2)]
public void Test2() { /* ... */ }

[Order(3)]
public void Test3() { /* ... */ }

However, you should really try to make sure your tests are self-contained otherwise they can be quite brittle.但是,您真的应该尝试确保您的测试是独立的,否则它们可能会非常脆弱。

Just use order attribute.只需使用order属性。

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Threading;

namespace FirstTestCase
{
    class _04_02_Media
    {

        class NUnitTest
        {
            [Test, Order(1)]
            public void Libraries()
            {}

            [Test, Order(2)]
            public void OnDemandReplace()
            {}

            [Test, Order(3)]
            public void OnDemandDelete()
            {}
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM