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

时间:2022-07-13 19:15:58

Is it possible to run specific method before each test in an assembly?

是否可以在程序集中的每个测试之前运行特定的方法?

I know about TestInitialize attribute 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属性,但是这个属性有“类作用域”。如果它是在一个测试类中定义的,那么它将在这个类的每个测试之前执行。

I want to define a method that will be executed before each test defined in a whole assembly.

我想定义一个方法,该方法将在整个程序集中定义的每个测试之前执行。

5 个解决方案

#1


11  

I am not sure that this feature is possible in MsTest out of box like in other test frameworks (e.g. MbUnit).

我不确定这种特性是否可以在其他测试框架(例如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... :)

可能还有另一种解决方案,您可以定制测试属性——但我还没有尝试过……:)

#2


54  

[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变量来验证这一点。

#3


13  

You want to use [AssemblyInitialize].

您需要使用[assembly yinitialize]。

See: MSDN Link

看:MSDN链接

or this question: on *

或者这个问题:关于*

#4


1  

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 TestInitialize is your attribute. By the way the best unit-test comparison is available at Link on Codeplex

编辑:如果它不工作(我仍然认为它应该工作,因为MSTest需要确保独立的测试方法运行),那么TestInitialize就是你的属性。顺便说一下,最好的单元测试比较可以在Codeplex上的Link上找到

#5


0  

I think you are looking for the ClassInitialize attribute.

我认为你在寻找ClassInitialize属性。

#1


11  

I am not sure that this feature is possible in MsTest out of box like in other test frameworks (e.g. MbUnit).

我不确定这种特性是否可以在其他测试框架(例如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... :)

可能还有另一种解决方案,您可以定制测试属性——但我还没有尝试过……:)

#2


54  

[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变量来验证这一点。

#3


13  

You want to use [AssemblyInitialize].

您需要使用[assembly yinitialize]。

See: MSDN Link

看:MSDN链接

or this question: on *

或者这个问题:关于*

#4


1  

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 TestInitialize is your attribute. By the way the best unit-test comparison is available at Link on Codeplex

编辑:如果它不工作(我仍然认为它应该工作,因为MSTest需要确保独立的测试方法运行),那么TestInitialize就是你的属性。顺便说一下,最好的单元测试比较可以在Codeplex上的Link上找到

#5


0  

I think you are looking for the ClassInitialize attribute.

我认为你在寻找ClassInitialize属性。