jeudi 29 janvier 2015

Is there a practical way to reuse a set of test methods against different stubs in the Microsoft Unit Test Framework?

I am using the Microsoft Unit Test framework under Visual Studio 2012 to test a C# class that represents a data access layer towards a database of some kind.


In order to be able to unit test it, I have created a stub that the class uses instead of said database. My test class has the following structure:



[TestClass]
public class UnitTests {

MyStub stub;

[TestInitialize]
public void InitializeContext() { // Construct stub }

[TestCleanup]
public void CleanupContext() { // Dispose stub }

[TestMethod]
public void Test1() { ... }

[TestMethod]
public void Test2() { ... }

...
}


It works fine.


I would now like to write a second test class that performs the same tests against a different stub, therefore I'd like to reuse the code, but I'm unable to achieve this through inheritance (inheriting UnitTests and overriding both InitializeContext() and CleanupContext()) because the TestMethod attribute appears not to be inheritable.


Is there a practical way to achieve this at all? If so, which one?


Aucun commentaire:

Enregistrer un commentaire