How to run the same test methods when changing dependencies?

I have 5 test methods that test the functionality of the PasswordManager object. I use the visual studio 2008 built-in test engine. This manager can use two dependencies: XMLStorageManager or DbStorageManager. The dependency is set in the Passwordmanager constructor. How can I run tests twice with the only difference that I use StorageManager?

(I know, I know, these are NOT unit tests ...)

+4
source share
3 answers

I am not an MSTest user, but you probably have several options. Normally with NUnit I would use a generic or parameterized device, but I'm not sure if MSTest has similar capabilities. In light of this, here, as I would do it with NUnit in a form that should be reproducible using any unit test structure using the template template template .

Steps:

  • Define an abstract base class using all tests in it
  • Insert the abstract CreateStorageManager () method, which returns the IStorageManager (or something else) interface of the two dependencies to implement)
  • Subclass twice and provide an implementation of CreateStorageManager (), which returns the specific type that you want to use to run the tests.

Here's the code for the equivalent version of NUnit; I'm sure you can extrapolate. Note. . The inheritance rules for MSTest may differ slightly from what I'm used to. If this does not work, you can try marking the base class as test equipment.

public abstract class PasswordManagerFixtureBase { protected abstract IStorageManager CreateStorageManager(); // all tests go in this fixture [Test] public void SomeTestOrOther() { var passwordManager = CreatePasswordManager(); // do test logic } private PasswordManager CreatePasswordManager() { // calls into subclass implementation to get instance of storage IStorageManager storage = CreateStorageManager(); return new PasswordManager(storage); } } // Runs the tests in the fixture base using XmlStorageManager [TestFixture] public class PasswordManager_XMLStorageManagerImplTests { protected override IStorageManager CreateStorageManager() { return new XMLStorageManager(); } } // Runs the tests in the fixture base using DbStorageManager [TestFixture] public class PasswordManager_DbStorageManagerImplTests { protected override IStorageManager CreateStorageManager() { return new DbStorageManager(); } } 

MSTest may be a more elegant way to do this, but it should work.

+5
source

If a PasswordManager has a dependency such as IStorageManager that is introduced (DI, IoC, etc.), then this may be enough to make fun of this interface instead of using a specific implementation, so there is no need to test PasswordManager for XML and Db implementations that can be tested separately from PasswordManager .

+1
source

Just think, but you could create an ordered test and add the same tests twice: add all the tests once, then all the tests will be in the same order again. In the test context, consider the number of attempts to run each test. I believe the context is static, so it should only be created once, and then reused as more tests are run. In the test setup, use XmlStorageManager if the number of tests is equal and DbStorageManager if the number of tests for this test is odd.

0
source

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


All Articles