📜 ⬆️ ⬇️

Unit tests and inheritance

When developing unit tests in Visual Studio, you often want to create some kind of base class for testing basic loniki. For example, we have the following class: [TestClass]
public virtual class PersonTestBase
{
[TestClass]
public virtual class PersonTestBase
{

[TestMethod]
public void GetNameTest ()
{
// ...
}
}
and its heir class: [TestClass]
public class CustomerTest: PersonTestBase
{
[TestMethod]
public override void GetNameTest()
{
base.GetNameTest();
}
}
[TestClass]
public class CustomerTest: PersonTestBase
{
[TestMethod]
public override void GetNameTest()
{
base.GetNameTest();
}
}

Advantages of this approach:Minuses:Immediately it should be noted that PersonTestBase and CustomerTest must be in the same assembly, otherwise tests in PersonTestBase will not work - this is a limitation of unit tests. See msdn for details. In addition to the methods described in msdn, you can do this: two projects are created: BaseTests and CustomTests; the necessary files from BaseTests are added to the CustomTests project in the following way: Project -> Add Existing Item -> Select the necessary files -> Add As Link. in different projects, but when compiling the necessary classes are in the same assembly. Now it's time to change our CustomerTest. [TestClass]
public class CustomerTest: PersonTestBase
{
[TestMethod]
public override void CustomerTestMethod()
{
//...
}
}

We added a new Customer-specific method and removed method overrides from the base class, because its functionality suits us completely. What we got from this:It's a shame, but not fatal. We are come to the rescue by a regular utility MSTest, which solves everything, or almost everything, our problems. The advantages of this method are:Minuses:

')

Source: https://habr.com/ru/post/47752/


All Articles