开发者

Can I write a test without any assert in it?

开发者 https://www.devze.com 2022-12-31 05:54 出处:网络
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.

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 s开发者_如何学编程ome db-configuration, I re-run this test and check if the configuration is right. ?

Thanks!


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.


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.


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.


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.


the purpose of the test is to check if "X" has an "expected something", in order to check that "expected something" is correct to assert, to expect or to verify. This is why most frameworks implements those methods in a way or another

0

精彩评论

暂无评论...
验证码 换一张
取 消