Should I write an integration test or unit test?

I have a function that saves photos (stored in a database, the application gives the user the ability to save in a directory) to this directory. Now this does not work correctly. I just fixed it. Now should I write a unit test or an integration test for a function?

+7
automated-tests
source share
5 answers

In your case, you want to write an integration test to cover the scenario you described. I have a complete entry in this section . However, here is a generalized version specific to your question:

In his book, The Art of Unit Testing, Roy Osherow describes the key principle that unit test should be "trustworthy." At first glance, this seems pretty obvious. However, this is based on the main differences between the unit test and the integration test.

With a reliable test, you should be able to trust the results in 100% of cases. If the test fails, you must be sure that the code is broken and needs to be fixed. You do not need to ask such things as β€œWas the database inaccessible?”, β€œWas the connection string OK?”, β€œWas the stored procedure changed?”. Asking these questions, he shows that you cannot trust the results, and you probably have a poorly designed "unit test".

As your scenario describes a situation with similar multiple dependencies, you want to cover it with an integration test. Again, for more information, see my full post here .

Good luck

+17
source share

Integration tests and unit tests have different areas and areas:

  • Unit tests test small fragments of code (for example, a function) in isolation from the rest of the program, ideally covering all possible cases of edges (for example, exceptions, null parameters, etc.).
  • Integration tests test the entire application in terms of use. They can never cover all cases of edges, but they can catch problems with the interaction between the pieces of code and the glue code that brings them together, which often test units of measure.

For one function, you really can only have a unit test, and you need to. But you can also have an integration test, which shows that when the user clicks a certain button, the photo is written to the directory and can also be opened in the program.

+8
source share
  • Integration tests will help you verify the correct operation of your software.
  • Unit tests will help you find out why your software crashes.

Unit tests to some extent also contribute to the achievement of the first goal. Plus, it has several advantages:

  • It is generally cheaper to write and run unit test with much less volume.
  • It is easier to get coverage for the combinatorial explosion of the states of your components using unit tests than an integration test. Say you have a setup with three components. Each of them has 3 different states. Then, integration testing of the entire installation would include checking 3 * 3 * 3 = 27 conditions. Testing individual components will require testing 3 + 3 + 3 = 9 conditions. (This is simplified, but you hopefully see this.)

Because of this, unit tests are generally more popular than integration tests. However, you cannot do without integration tests. Integration tests should be the cornerstone used to host your software. Having unit tests only proves that you have a bunch of things that do something. The integration test proves that you have working software.

+5
source share

Some people would call the DAO test an integration test; others would say this is unit test.

Whatever you call it, I would say that you should have a unit test for all the DAO functionality and an integration test for front-to-back behavior embodied in a use case that says: β€œAllow the user to save to the file system . " I would have integration tests for both scenarios, since it is possible on your system that both options are possible.

+1
source share

I think it depends on the source of your problem. If the function itself may have some problems in different scenarios, you can conduct unit tests to test these scripts on your function. If integrating your function and other parts of your program may cause some problems, you should consider an integration test. Sometimes functions like yours may need some external resources to do their job. It is a good idea to have some unit tests to see what happens if some of these resources are not available.

+1
source share

All Articles