Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Has unit/integration test without asserts right to exist?

Tags:

unit-testing

Recently I had to chance to see a bunch of tests without any assert. Those tests had to be accuracy tests.

I think the only thing that can be tested with such tests is to check if none exceptions are raised during execution flow. But anyway I really don't understand how we can verify the accuracy of the code with no asserting tests - even the methods that do nothing can pass such tests.

So I'm wondering what can be purpose of such tests and what else can be tested with that?

like image 842
DixonD Avatar asked Aug 06 '10 13:08

DixonD


People also ask

Do all unit tests need assert?

Tests should always "prove" something, but that does not require an "assert" statement.

What takes place when no assertion is added to a Junit test?

A test without an assert (containing only an action with a possible setup) makes sure that the process works without an exception. In any other case, the test passes.

Can we do integration testing without unit testing?

Unit testing and integration testing are both important parts of successful software development. Although they serve different yet related purposes, one cannot replace the other.

What is the main difference between a unit test and an integration test?

Unit Testing is a kind of white box testing, whereas Integration Testing is a kind of black-box testing. For Unit Testing, accessibility of code is required, as it tests the written code, while for Integration Testing, access to code is not required, since it tests the interactions and interfaces between modules.


2 Answers

I'd let them have the right to exist if they are testing some "legacy code" - code that no one really has a good grip on. No, if they are in there to put on a show of "high code coverage".

It's a first step to getting some confidence in the code. The invisible check is :"no exceptions are thrown". However once you've wrapped up the app in some smoke tests like these, the next step would be to turn the implicit check into an explicit check - put on your thinking cap and come up with observable changes that you can assert from your tests.
You can use tools like TestLint to catch tests like these.

like image 73
Gishu Avatar answered Oct 10 '22 01:10

Gishu


Here is an example (C#, NUnit):

[Test]
public void StartUpShutDownDoesntThrow() {
   var aService = new Service();
   aService.StartUp();
   aService.ShutDown();
}

If this test passes it ensures the following:

  • The constructor doesn't throw an exception
  • Startup() doesn't throw an exception
  • ShutDown() doesn't throw an exception

All three items are valuable, maybe even epic. You woud be surprised how many commercial application don't even have something as basic as this as part of their QA process.

So, yes, tests without asserts can have their value, too.

like image 29
Manfred Avatar answered Oct 10 '22 00:10

Manfred