Should each testing method at least approve?

When I test the void method, there is nothing to say. For example, the CreateSomething method. I know that I could call another method in the testing method, for example FindSomething, but in any case, if there is an error (in the create method), this will be displayed. So is it good practice to invoke a statement in each method, or am I fine sometimes without a statement?

+4
source share
8 answers

Not necessarily confirmation

But your test code must do at least one of the following:

  • state that any property / result has / has not been set to a specific value
  • verify that certain methods have been called / excluded
  • make sure exceptions behave (fire or not) as expected

So these are the values, actions and errors that you should check. Sometimes only one of them, sometimes you cannot do it without a combination.

+5
source

Invalid methods often change the state of an instance. In this case, your testing method should state that the expected state is present after the call. That is, you need to state the status of the respective members.

Invalid methods without side effects can also be tested using the layout. In this case, you will verify that the method makes the expected calls for the mock object.

Having said that functions similar to methods should be preferable to IMO, since they are easier to reason and easier to test, but this is only my opinion.

+4
source

When I test the void method, there is nothing to say.

So what is the purpose of the method?

The answer to this question helps to find what needs to be argued. If anwser is really nothing, you can remove this method from your code without any consequences.

Implementing test code to cover this claim is another issue that may or may not be simple or relevant given your development environment or project limitations.

+2
source

There is no need to have an assert in every testing method. For example, if you want to verify that your method will throw an exception, you can use ExpectedException (MS Test). If you do not check that your method throws an exception and you do not have one assert , you may do something wrong. Just calling a method is not a good unit test. You need to make sure somehow if this method executes as you expect after calling it, which is usually achieved through approval.

0
source

Well, if you only need to check if this method works ... wrap it in try and catch, and if for some reason (and you can't say anything) - assert (false) in catch, or if you expect exceptions - use ExpectedException ...

0
source

You must be sure to state at least one fact in each test. Just because your unit test framework will count the number of statements, and the number of test measures / number of statements can give a good first impression of the test suite.

If you seem to be unable to say anything: all unit test frameworks that I know have Assert.Throws/Assert.DoesNotThrow for this purpose.

0
source

Of course there is something like Assert.Throws / Assert.DoesNotThrow in MSTest there are Assert.Fail () and AssertFailedException ()

0
source

Make sure this fails if your testing fails. You can find out about this:

  • claim
  • exception thrown
  • The mock object complains that it did not get the correct name.

There may be some exceptions to this, but I canโ€™t think of anything. One TDD principle is important here:

First write a failed test.

If you do, you guarantee him a good test.

Some people argue that each test should have one and only one, to argue ... but that's another question.

0
source

All Articles