.net MsTest - 在程序集中的每个测试之前执行方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/639326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MsTest - executing method before each test in an assembly
提问by brzozow
Is it possible to run specific method before each test in an assembly?
是否可以在程序集中的每次测试之前运行特定方法?
I know about TestInitializeattribute but this attribute has "class scope". If it's defined in a Test class it will be executed before each test from this class.
我知道TestInitialize属性,但此属性具有“类范围”。如果它是在 Test 类中定义的,它将在该类的每个测试之前执行。
I want to define a method that will be executed before each test defined in a whole assembly.
我想定义一个方法,该方法将在整个程序集中定义的每个测试之前执行。
采纳答案by nihique
I am not sure that this feature is possible in MsTest out of box like in other test frameworks (e.g. 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. In your case, every test class in your assembly must inherit from this base...
如果我必须使用 MsTest,那么我将通过定义具有 [TestInitialize] 属性的抽象类 TestBase 来解决这个问题,并且每个需要此行为的测试都源自此基类。在您的情况下,程序集中的每个测试类都必须从这个基类继承......
And there is probably another solution, you can make your custom test attribute - but I have not tried this yet... :)
可能还有另一种解决方案,您可以创建自定义测试属性 - 但我还没有尝试过... :)
回答by FryGuy
[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 变量来验证这一点。
回答by Robert
You want to use [AssemblyInitialize].
您想使用[AssemblyInitialize].
See: MSDN Link
请参阅:MSDN 链接
or this question: on stackoverflow
或者这个问题: 在stackoverflow上
回答by David Pokluda
Well isn't MSTest instantiating the class for each test? That was my understanding of it. In such a case whatever you call from your constructor is the initialization code (per test by definition).
那么 MSTest 不是为每个测试实例化类吗?这是我对它的理解。在这种情况下,无论您从构造函数调用什么,都是初始化代码(根据定义每个测试)。
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 TestInitializeis your attribute. By the way the best unit-test comparison is available at Link on Codeplex
编辑:如果它不起作用(我仍然认为它应该起作用,因为 MSTest 需要确保单独的测试方法运行是隔离的)然后TestInitialize是您的属性。顺便说一下,最好的单元测试比较可以在Codeplex上的Link 上找到
回答by Andrew Hare
I think you are looking for the ClassInitializeattribute.
我认为您正在寻找该ClassInitialize属性。

