Single Test Data Warehouse

Suppose I have an interface with the storeData (key, data) 'and' getData (key) 'methods. How to check a specific implementation? Should I check if the data on the data medium (e.g. sql database) is set correctly, or should I just check if they returned the correct data using getData?

If I look at the data in the database, it seems to me that I am also testing the internals of the method, but I check to see if it gives the same data back that it is incomplete.

+6
unit testing
source share
5 answers

It seems like you are getting into the hype of unit testing, what you will do is actually an integration test. Setting and returning the same value from the same key is the unit test that you do with the mock implementation of the storage engine, but actually check the real store, say your database, as you would expect, which is no longer is a unit test, but this is a fundamental part of testing, and for me it is like integration testing. Do not use unit testing as a hammer ; select the tools you need for proper operation. Divide your testing into more layers.

+2
source share

What you want to do in unit test, make sure that the method does the job that it should do. If a method uses dependencies to do this work, you will mock these dependencies and make sure that your method calls methods on the objects it depends on with the appropriate arguments. This way you check your code separately.

One of the advantages of this is that it will drive the design of your code in a better direction. For example, to use mockery, you naturally gravitate towards more untied code using dependency injection. This gives you the ability to easily replace your mock objects with the actual objects your class depends on. You also end up implementing interfaces that are more naturally ridiculed. Both of these are good design patterns and will enhance your code.

To test your specific example, for example, your class may depend on the factory for creating database connections and the builder for creating parameterized SQL commands that are executed through the connection. You will pass these mocked versions of these objects to your class and make sure that the correct methods for setting up the connection and command, assembling the correct command, executing it, and breaking the connection were installed. Or perhaps you add an already open connection and just create a command and call it. The point is that your class is built against an interface or a set of interfaces, and you use the mockery of supplying objects that implement these interfaces, and can record calls and provide the correct return values ​​to the methods that you expect to use from the interface (s).

+2
source share

In such cases, I usually create SetUp and TearDown methods that fire before / after my unit tests. These methods will configure any test data that I need in db, and delete any test data when I do this. Pseudo-code example:

Const KEY1 = "somekey" Const VALUE1= "somevalue" Const KEY2 = "somekey2" Const VALUE2= "somevalue2" Sub SetUpUnitTests() { Insert Into SQLTable(KEY1,VALUE1) } //this test is not dependent on the setData Method Sub GetDataTest() { Assert.IsEqual(getData(KEY1),VALUE1) } //this test is not dependent on getData Method Sub SetDataTest() { storeData(newKey,NewData) Assert.IsNotNull(Direct Call to SQL [Select data from table where key=KEY2]) } Sub TearDownUnitTests() { Delete From table Where key in (KEY1, KEY2) } 
+1
source share

I think it depends on what happens to the data later - if you only ever access the data using storeData and getData , why not test the methods in agreement? I suppose there is a chance that an error will occur and it will be a little harder to figure out if it is in storeData or getData , but I think the risk is acceptable if it

  • simplifies test execution and
  • hides the insides as you say

If the data is read or pasted into the database using some other mechanism, I would check the database using SQL, as you suggest.

@brendan makes a good point though - whatever method you choose, you will insert data into the database. It is a good idea to clear the data before and after the tests to ensure consistent results are achieved.

0
source share

Testing at a concert is a common technique (at least in my experience), and I will not shy away from it. I used the same template for serialization / deserialization, parsing and printing. A.

If you do not want to hit the database, you can use the database layout. Some people have the same feelings as you when using ridicule - it partly depends on the implementation. As with all things, this is a trade-off: consider the benefits of ridicule (faster, not db-dependent) on its flaws (it won't detect actual problems with db, slower).

0
source share

All Articles