Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I write a test without any assert in it?

I'd like to know if it is "ok" to write a test without any "assert" in it. So the test would fail only when an exception / error has occured.

Eg: like a test which has a simple select query, to ensure that the database configuration is right. So when I change some db-configuration, I re-run this test and check if the configuration is right. ?

Thanks!

like image 916
stratwine Avatar asked May 24 '10 20:05

stratwine


People also ask

Should a unit test only have one assert?

And usually, one test method only has one assert at the end of it. But what happens if you have multiple asserts in a unit test? If you have more than one assert in a unit test, the unit test will be completed successfully if all assertions are met.

Why do we use assert in testing?

Assertions are used for validating a test case and helps us understand if a test case has passed or failed. The assertion is considered to be met if the actual result of an application matches with that of the expected result.

How do you write test assertions?

Assert the exact desired behavior; avoid overly precise or overly loose conditions. One assertion, one condition. Don't aggregate multiple checks in one assertion. Write assertions that check requirements, not implementation details of the unit under test.

How many assertion should a test case contain?

Some simple rules that I can think of off hand for how many assert's to put in a test case: Don't have more than one assert that depends on the side-effects of previous execution.


2 Answers

It is perfectly valid to make sure a unit test runs without encountering an exception.

As per Matt B's suggestion, be sure to document what the test is actually testing to be clear and precise.

like image 92
Kyle Rosendo Avatar answered Oct 18 '22 02:10

Kyle Rosendo


As @Kyle noted, your test case is valid. In fact the opposite would also be a valid: when you write a test case to confirm that a certain call with specific parameter(s) results in an exception.

like image 33
Péter Török Avatar answered Oct 18 '22 00:10

Péter Török