4

I'm wanting to create a load of unit tests to make sure my stored procedures are working, but i'm failing (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 because the tests don't run sequentially, tests 2 and 3 fail because the global variable isn't set.

Advise? :'(

1
  • 3
    You should be aiming to write tests that aren't dependent on each other. Any setup/teardown required for the tests should be performed first. As an aside, if you're actually calling out the the database, this is more of an integration test than a unit test, since you're testing both the database and the access code at the same time. Commented Apr 1, 2011 at 14:58

4 Answers 4

3

The key word here is 'unit'.

A unit test should be self-contained, i.e. be comprised of the code to perform the test, and should not rely on other tests being executed first, or affect the operation of other tests.

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

Sign up to request clarification or add additional context in comments.

Comments

1

Check out the TestInitializeAttribute. You would place this on a method that should run before every test to allocate the appropriate resources.

One side note since it looks like you're misinterpreting how these should work: Unit tests should not require artifacts from other tests. If you're testing modifications, the initialize / setup method(s) should create the space that's to be modified.

Comments

1

Check out Why TestInitialize gets fired for every test in my Visual Studio unit tests?

I think that will point you in the correct direction. Instead of running it as a test, you could run it as a TestInitialize.

There are 'Ordered tests' but it breaks the idea of each test running independent.

2 Comments

Yeah unfortuantely for this particular type of testing I need them to be ordered. could you point me in the right direction?
@Karl Stoney: Check out msdn.microsoft.com/en-us/library/ms182631.aspx and codeproject.com/KB/testing/UnitTestVS2008.aspx Those cover "Ordered Testing" I haven't used them, but the name sounds like what you're looking for. :)
0

First off, the test you describe doesn't sound like a unit test, but more like an integration test. A unit test is typically testing a unit of functionality in your code, isolated from the rest of the system, and runs in memory. An integration test aims at verifying that the components of the system, when assembled together, work as intended.
Then, without going into the details of the system, it looks to me that I would approach it as one single test, calling multiple methods - something along the lines of:

[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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.