views:

215

answers:

5

Hi,

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!

+9  A: 

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.

Kyle Rozendo
just be sure document what the test is actually testing to be clear
matt b
@kyle thanks !@matt Yes, I would ! :) (The example I gave defenitely requires that )
stratwine
+2  A: 

Testing is a really subjective discussion. Some people will say no, you should always have AAA syntax. Personally I've written tests that do things very similar to what your talking about so I'd say, sure go ahead - if it helps you build a more stable app then why not.

For example in NUnit i consider [ExpectedException typeof(XXXX)] to be logically equivalent to an Assert.

Also in some tests you might not assert anything but expect a particular order of execution via Mocks and Expects.

JoshReedSchramm
@Josh thanks ! Yes. I'm reminded of seeing some JMock tutorial without asserts but Expectations. Will explore on that !
stratwine
+4  A: 

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.

Péter Török
+2  A: 

Sure you can do that. It is also perfectly fine to write a test without assertions where the expected outcome is an exeption. I know testng will let you specify an exception that should be thrown and the test will fail if the expected exception isn't thrown.

Mattias Nilsson
A: 

It is surely acceptable to write a unit test that doesn't have any assertions. You could do this for:

  • Testing a case that ends without an exception. In this case, if you can, it's nice to dress the test with the specific type of the exception, as in [ExpectedException(MyException)].

  • Testing a feature is there. Even there isn't a possibility that the test may generate an exception, you may want to make this test fail if someone decides to remove that feature. If the test uses a method and the method is removed, the test will simply fail to build.

Gorkem Pacaci