简体   繁体   English

MsTest - 在程序集中的每个测试之前执行方法

[英]MsTest - executing method before each test in an assembly

Is it possible to run specific method before each test in an assembly?是否可以在程序集中的每次测试之前运行特定方法?

I know about TestInitialize attribute but this attribute has "class scope".我知道TestInitialize属性,但该属性具有“类范围”。 If it's defined in a Test class it will be executed before each test from this class.如果它是在 Test 类中定义的,它将在该类的每个测试之前执行。

I want to define a method that will be executed before each test defined in a whole assembly.我想定义一个方法,该方法将在整个程序集中定义的每个测试之前执行。

[TestInitialize()] is what you need. [TestInitialize()]正是您所需要的。

private string dir;

[TestInitialize()]
public void Startup()
{
    dir = Path.GetTempFileName();
    MakeDirectory(ssDir);
}

[TestCleanup()]
public void Cleanup()
{
    ss = null;
    Directory.SetCurrentDirectory(Path.GetTempPath());

    setAttributesNormal(new DirectoryInfo(ssDir));
    Directory.Delete(ssDir, true);
}


[TestMethod]
public void TestAddFile()
{
    File.WriteAllText(dir + "a", "This is a file");
    ss.AddFile("a");
    ...
}

[TestMethod]
public void TestAddFolder()
{
    ss.CreateFolder("a/");
    ...
}

This gives a new random temporary path for each test, and deletes it when it's done.这为每个测试提供了一个新的随机临时路径,并在完成后将其删除。 You can verify this by running it in debug and looking at the dir variable for each test case.您可以通过在调试中运行它并查看每个测试用例的 dir 变量来验证这一点。

I am not sure that this feature is possible in MsTest out of box like in other test frameworks (eg MbUnit).我不确定在开箱即用的 MsTest 中是否可以像在其他测试框架(例如 MbUnit)中一样使用此功能。

If I have to use MsTest, then I am solving this by defining abstract class TestBase with [TestInitialize] attribute and every test which needs this behaviour derives from this base class.如果我必须使用 MsTest,那么我将通过定义具有[TestInitialize]属性的抽象类 TestBase 来解决这个问题,并且每个需要此行为的测试都源自此基类。 In your case, every test class in your assembly must inherit from this base...在您的情况下,程序集中的每个测试类都必须从这个基类继承......

And there is probably another solution, you can make your custom test attribute - but I have not tried this yet... :)可能还有另一种解决方案,您可以制作自定义测试属性 - 但我还没有尝试过... :)

You want to use [AssemblyInitialize] .您想使用[AssemblyInitialize]

See: MSDN Link请参阅: MSDN 链接

or this question: on stackoverflow或者这个问题: 在stackoverflow上

Well isn't MSTest instantiating the class for each test?那么 MSTest 不是为每个测试实例化类吗? That was my understanding of it.这是我对它的理解。 In such a case whatever you call from your constructor is the initialization code (per test by definition).在这种情况下,无论您从构造函数调用什么,都是初始化代码(根据定义每个测试)。

EDIT: If it doesn't work (which I still think it should because MSTest needs to make sure that individual test method runs are isolated) then TestInitialize is your attribute.编辑:如果它不起作用(我仍然认为它应该起作用,因为 MSTest 需要确保单独的测试方法运行是隔离的)然后TestInitialize是您的属性。 By the way the best unit-test comparison is available at Link on Codeplex顺便说一下,最好的单元测试比较可以在Codeplex上的Link 上找到

我认为您正在寻找ClassInitialize属性。

暂无
暂无

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

相关问题 用于检查收集的MSTest测试方法 - MSTest Test Method to check for collection 在Visual Studio单元测试(MSTest)中对项目中的所有测试进行每次测试之前,是否可以执行初始化代码? - Is there a way to execute initializer code before each test in Visual Studio Unit Testing (MSTest) for all tests in a project? 我可以强制MSTest为每次测试运行使用新进程吗? - Can I force MSTest to use a new process for each test run? 如何在每个 class 之后运行 ClassCleanup (MSTest) 并进行测试? - How to run ClassCleanup (MSTest) after each class with test? 在 MSTest 中每个测试用例之后清除所有 static 数据 - Clear all static data after each test case in MSTest MSTest:如果在测试程序集中使用嵌套类,则“无法加载测试容器”错误 - MSTest: `Unable to load test container` error if nested classes are used in test assembly 在 .NET 中执行测试之前清理数据库 - Clean database before executing a test in .NET 如何在所有测试之前执行一行代码,这是MSTest中的数据设置代码 - How to execute a line of code which is a data setup code in MSTest before all test 在MSTest中,如何指定某些测试方法不能彼此并行运行? - In MSTest, how can I specify that certain test methods cannot be run in parallel with each other? 使用mstest,我可以针对我支持的每种语言运行单元测试套件吗? - With mstest, can I run my unit test suite against each of my supported languages?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM