我一直在做一些关于测试驱动开发的研究,发现它非常酷.
我遇到的一件事是,当你编写测试时,有一个执行你的设置和测试方法的顺序([Setup]和[Test]).
是否还有其他可以在测试时使用的内容,如果是,那么执行顺序是什么,例如dispose或其他什么?我看到了测试夹具设置,但不太熟悉那个.
例:
当我运行测试时,它先执行[Setup]然后运行[Test],当它进入下一个测试时再次运行[Setup],然后进入[Test].
如果有帮助,我正在使用NUnit.
这是我设置的截断示例:
using NUnit.Framework;
namespace TestingProject
{
[TestFixture]
public class CustomerService_Tests
{
public string MyAccount = string.Empty;
[SetUp]
public void Setup()
{
MyAccount = "This Account";
}
[Test]
public void Validate_That_Account_Is_Not_Empty()
{
Assert.That(!string.IsNullOrEmpty(MyAccount));
}
[Test]
public void Validate_That_Account_Is_Empty()
{
Assert.That(string.IsNullOrEmpty(MyAccount));
}
}
}
Run Code Online (Sandbox Code Playgroud)
因此,当我运行测试时,它会进行设置,然后进行第一次测试,然后进行设置,然后进行第二次测试.
我的问题是我在测试时可以使用的其他类型如[Setup]和[Test]以及这些类型的执行顺序是什么.
Cod*_*ker 21
使用NUnit(不确定其他人),您有以下执行顺序:
TestFixtureSetup
建立
测试
拆除
建立
测试
拆除
TestFixtureTearDown
每次运行测试时,它总是按顺序执行.
如果您查看以下代码,您可以看到我正在谈论的完全复制品.您甚至可以复制并粘贴此代码,它应该可以使用(使用NUnit,不确定它是否可以与其他人一起使用).
如果在调试模式下运行此命令,并在每个方法上设置断点,则可以在调试时查看执行顺序.
using NUnit.Framework;
namespace Tester
{
[TestFixture]
public class Tester
{
public string RandomVariable = string.Empty;
[TestFixtureSetUp]
public void TestFixtureSetup()
{
//This gets executed first before anything else
RandomVariable = "This was set in TestFixtureSetup";
}
[SetUp]
public void Setup()
{
//This gets called before every test
RandomVariable = "This was set in Setup";
}
[Test]
public void MyTest1()
{
//This is your test...
RandomVariable = "This was set in Test 1";
}
[Test]
public void MyTest2()
{
//This is your test...
RandomVariable = "This was set in Test 2";
}
[TearDown]
public void TestTearDown()
{
//This gets executed after your test gets executed.
//Used to dispose of objects and such if needed
RandomVariable = "This was set in TearDown";
}
[TestFixtureTearDown]
public void TestFixtureTearDown()
{
//Executes Last after all tests have run.
RandomVariable = "This was set in TestFixtureTearDown";
}
}
}
Run Code Online (Sandbox Code Playgroud)