Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am using Visual Studio Unit test framework for writing the unit tests to verify existing base data in the database. The data is very important to our team and we want to make sure at all times that the data is not changed.

Any guidance on how to write these type of tests?

share|improve this question
3  
In that case, it's not a unit test. A unit test is only for a specific piece of isolated code to make sure it's working as desired. This is more likely an integration test. – mason 3 hours ago
1  
If the data can't change, why is it in the database instead of being hard coded? Also, couldn't you use database constraints to enforce the data existing instead of software tests? – Andrew Piliser 3 hours ago
up vote 1 down vote accepted

Depending on the base data volume you may consider one of the following:

Small volume

Each test may fetch all data from a table and check that you find what you expect


Relatively large volume

You can construct a reference database containing all the tables and data that should always be in your database. You can execute queries to check that reference information is there:

-- checks that used table is not missing any reference table information
-- if count > 1 then something is missing
SELECT COUNT(1)
FROM RefDb.TheTable RT
WHERE NOT EXISTS (SELECT 1 FROM ActualDb.TheTable AT
   WHERE AT.PkId = RT.PkId  
   -- join can be extended for all interesting column values
) 

This solution is much faster that the first one for larger volume of data, as information is not fetched into the application layer. It also, allows an isolation between reference and used data, thus more security.

Another option is to store the information in a separate schema, but this means that reference information will replicated along with actual data for all environments (test, preprod etc.)

share|improve this answer

Your question is quite broad, however. There's nothing to stop you writing a test which retrieves the data from your database, you want to check for (ADO.NET might be a quick way to do it). You could use some kind of object comparison library to check records or do something simple like:

Assert.IsTrue(result.Any(x => x.Name == "John"));

Make sure you dispose of any db connection in your test.

share|improve this answer

Just test it. Use the unit test and write code that access the database and verify (assert) the result. Usually hitting the database is not common in unit tests. since usually you don't test the data you test the behavior how the software runs and acts. Because unit test should run fast you usually avoid to hit the database and stub or mock out the expected data. If you want to test if the expected data actually lands in the database - you would do integration tests. But like mentioned before this is more to test the code and the behavior of your application.

In your case you should implement some logic in you application that verifies the presence of your data in the database before start-up. Just code your would write. Same as your business logic - since it is so important it should be part of the business logic. And remember unit test is just code.

Don't know whether I did understand your question correctly but I hope this helps. Otherwise please edit your question =)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.