vendredi 27 mai 2016

How to set up one unit test that is dependent on another?

Is there a way in xUnit.net to make a unit test dependent on another so that it is skipped if the other one fails?

Note: This is not a question about shared fixtures between tests or making tests coupled to each other etc. I avoid that.

Example

Given:     Unit tests BarTest and FooTest
Condition: If BarTest is dependent on FooTest and FooTest fails
Result:    BarTest is skipped (ideally with an explanation)

Rationale

This is coming up as part of unit testing serialization, but I can think of other use cases as well. I have a type Bar that takes a dependency of type Foo where Foo is serialized as part of the serialization of Bar. I have a serialization unit test for type Foo, and if that fails I know that the serialization unit test for Bar will fail due to nesting.

The types look sort of like this:

[Serializable]
class Foo {}

[Serializable]
class Bar {

    public Bar(Foo foo) {
        Foo = foo;
    }

    [Serializable]
    Foo Foo { get; }
}

  • I don't want both tests to fail because that makes it difficult to find the root cause of the failure. I Think it is better if the unit test for Bar is not run with a warning of the dependency failure.

  • I really don't want to use a Stub for Foo in the unit test for Bar because that forces me to introduce an interface for Foo and clutter up a hierarchy where only a single concrete implementation of these types are necessary. It also forces me to create a custom serializer for the stub which sounds like more trouble than the benefit.

Questions

I have not found any information on this use case after searching for a while, therefore I have the following questions:

  1. Is it possible to create tests that are running conditionally based on the result of other unit tests in xUnit.net?
  2. Is this a bad request in any way or a code smell, and if so, why?

Aucun commentaire:

Enregistrer un commentaire