Testing modules in Visual Studio

I want to create a unit test load to make sure my stored procedures work, but I fail (I'm new to tests in visual studio).

Basically I want to do the following:

<testclass()> Dim myglobalvariable as integer <testmethod()> Public sub test() -> use stored procedure to insert a record set myglobalvariable = result from the sp end sub public sub test2() -> use a stored procedure to modify the record we just added end sub public sub test3() -> use a stored procedure to delete the record we just added end sub end class 

The problem is that the tests are not executed sequentially, tests 2 and 3 fail because the global variable is not set.

Advise ?: '(

+4
source share
4 answers

The key word here is "unit".

A unit test should be autonomous, i.e. consist of code to run a test and should not rely on other tests to be performed in the first place or affect the operation of other tests.

See the list of anti-TDD patterns here for things to avoid when writing tests. http://blog.james-carr.org/2006/11/03/tdd-anti-patterns/

+3
source

Check out TestInitializeAttribute . You would place this on a method that must be run before each test in order to allocate the appropriate resources.

One-sided remark, since it looks like you are misinterpreting how this should work: unit tests do not require artifacts from other tests. If you are testing modifications, the initialization / installation method should create the space that needs to be changed.

+1
source

Check out Why does TestInitialize run for each test in Visual Studio unit tests?

I think this will point you in the right direction. Instead of running as a test, you can run it as TestInitialize .

There are "Ordered Tests", but this breaks the idea of ​​each independent test.

+1
source

Firstly, the test you described is not like a unit test, but rather as an integration test. A unit test usually tests the unit of functionality of your code, isolated from the rest of the system, and runs in memory. The integration test aims to verify that the components of the system, when assembled together, are working as intended.
Then, without going into details of the system, it seems to me that I approach it as one test, calling several methods - something like strings:

 [Test] public void Verify_CreateUpdateDelete() { CreateEntity(); Assert that the entity exists UpdateEntity(); Assert that the entity has been updated DeleteEntity(); Assert that the entity has been deleted } 
0
source

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


All Articles