How to order methods of execution using Visual Studio to do integration testing?

后端 未结 2 1338
猫巷女王i
猫巷女王i 2020-12-29 06:39

I have 2 questions in regard doing integration testing using VS 2010

First, I\'m really in need of finding a way to execute these testing methods in the order I want

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-29 07:09

    It is best practice to use functions to set up the tests and to clean them up, by using the attributes [TestInitialize] and [TestCleanUp] or [ClassInitialize] and [ClassCleanup].
    http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting(v=VS.100).aspx

    The next code is an example of a similar thing to what you want:

    [TestClass]
    public class UnitTest1
    {
        int i=0;
    
        [TestInitialize]
        public void Setup()
        {
            i = 5;
        }
    
        [TestMethod]
        public void TestMethod1()
        {
            Assert.AreEqual(5, i);
        }
    }
    

    The function SetUp will be called before executing each test. If you need to pass the value from one test to the other you might want to consider using a static variable which is not recommended due to the indeterministic order of execution.

    Usually there is a way to avoid needing a specific order by using the setup/cleanup technique, but it is true that this might not be true for very complex integration tests.
    If there is no possible way to avoid having them to reorder you can consider merging them in one, breaking again the best practice of having only one assert per test, but if they are so much dependent one from the other it might be even better this way, as in this case one test failing might compromise the result of the others.

    EDIT: May be using ordered tests answers question 1, and using static variables question 2: http://msdn.microsoft.com/en-us/library/ms182631.aspx

提交回复
热议问题