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!
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With