Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microsoft unit testing. Is it possible to skip test from test method body?

So I have situation when I need skip current test from test method body. Simplest way is to write something like this in test method.

if (something) return; 

But I have a lot complicated tests and I need a way to skip test from methods which I invoke in current test method body. Is it possible?

like image 746
DrunkCoder Avatar asked Jul 25 '12 12:07

DrunkCoder


People also ask

Can we skip unit testing?

skip=true to skip the entire unit test. By default, when building project, Maven will run the entire unit tests automatically. If any unit tests is failed, it will force Maven to abort the building process. In real life, you may STILL need to build your project even some of the cases are failed.

Does every method need a unit test?

The answer to the more general question is yes, you should unit test everything you can. Doing so creates a legacy for later so changes down the road can be done with peace of mind. It ensures that your code works as expected. It also documents the intended usage of the interfaces.

What do you have to avoid in tests in unit testing?

Avoid Test Interdependence You, therefore, cannot count on the test suite or the class that you're testing to maintain state in between tests. But that won't always make itself obvious to you. If you have two tests, for instance, the test runner may happen to execute them in the same order each time.

Are unit tests always necessary?

Unit tests are also especially useful when it comes to refactoring or re-writing a piece a code. If you have good unit tests coverage, you can refactor with confidence. Without unit tests, it is often hard to ensure the you didn't break anything.


1 Answers

You should not skip test this way. Better do one of following things:

  • mark test as ignored via [Ignore] attribute
  • throw NotImplementedException from your test
  • write Assert.Fail() (otherwise you can forget to complete this test)
  • remove this test

Also keep in mind, that your tests should not contain conditional logic. Instead you should create two tests - separate test for each code path (with name, which describes what conditions you are testing). So, instead of writing:

[TestMethod] public void TestFooBar() {    // Assert foo    if (!bar)       return;    // Assert bar } 

Write two tests:

[TestMethod] public void TestFoo() {    // set bar == false    // Assert foo }  [Ignore] // you can ignore this test [TestMethod] public void TestBar() {    // set bar == true    // Assert bar } 
like image 147
Sergey Berezovskiy Avatar answered Sep 28 '22 19:09

Sergey Berezovskiy