Organization unit test in a test class

Suppose I have several unit tests in a test class ([TestClass] in VSUnit in my case). I try to check only one thing in each test (does not mean only one Assert, though). Imagine one test (for example, Test_MethodA ()), which also checks the method used in other tests. I do not want to put the statement of this method in other tests that use it to avoid problems with duplication / maintainability, so I have a statement only in this test. Now, when this test fails, all tests that depend on the correct execution of this test method also fail. I want to find the problem faster, so I want to point to Test_MethodA somehow. It will be, for example, help,if I can make some of the tests in the test class run in a specific order, and when they fail, I will start looking for the cause of the failure in the first failed test. Do you know how to do this?

Edit: suggesting that the solution was to run the tests in a specific order, I probably went too far and in the wrong direction. I don't care about the test order. It is just that some tests will always fail if the prefix is ​​invalid. For instance. I have a test class that tests the DAO class (normally, maybe not a UNIT test, but there the logic is stored in the database stored procedures, which should be checked, but this is not the way I think). I need to insert some records into a table to verify that the method responsible for retrieving the records (let it call GetAll ()) gets them all in the correct order, for example. I am doing an insert using the DAO class method. Let me call it Insert (). I have tests that verify that the Insert () method works like this,as was expected. Now I want to test the GetAll () method. To get the database in the right state, I use the Insert () method. If Insert () does not work, most of the tests for GetAll () will fail. I would prefer to mention those tests that cannot pass, because Insert () does not work as unconvincing than unsuccessful. This will make it easier to find the cause of the problem if I know which method / test to look for first.

+3
3

, method_A() , , . , method_A() failure.

assert("method_A() returned true", true, rc);

, method_A() , .

, _() , NULL, . guard; , C, ++, NullPointerException.

+1

( ) . - - , , , unit test . , , , xUnit.net .

, , , , ( ).

, , Back Door .

(, ), .

:

public class MyClass
{
    public virtual void FirstMethod() { // do something... }

    public void SecondMethod() {}
}

FirstMethod , MyClass . . Moq :

var sutStub = new Mock<MyClass>();
// by default, Moq overrides all virtual methods without calling base

// Now invoke both methods in sequence:
sutStub.Object.FirstMethod(); // overriden by Moq, so it does nothing
sutSutb.Object.SecondMethod();
+3

, . - , . , .

With Visual Studio, you can order your tests: see here . But I would like to advise you to avoid this technique as much as possible: unit tests are designed to work anywhere and anytime, in any order.

EDIT: why is this a problem for you? All failed tests in any case point to the same method ...

0
source

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


All Articles