mercredi 31 août 2016

How to intentionally fail all C# unit tests

QUESTION:

I'm trying to figure out if there's a way to fail all C# unit tests whenever a particular condition is met.

BACKGROUND:

I set up a unit test for an object which encodes and decodes its internal data. Here's a fairly contrived example:

[TestClass]
public class FooTests
{
    private Foo TestFoo { get; set; }

    [TestMethod]
    public void DataEncodingIsWorking()
    {
        // TestFoo.EncodeData() ...
    }

    [TestMethod]
    public void DataDecodingIsWorking()
    {
        // TestFoo.DecodeData() ...
    }

    public FooTests(dynamic[] data) {
        TestFoo = new Foo(data);
    }
}

public class Foo {

    public void EncodeData() {
        // encodes Foo's data
    }

    public void DecodeData() {
        // decodes Foo's encoded data
    }

    public Foo(dynamic[] data) {
        // feeds data to Foo
    }
}

Rather than creating a new instance of TestFoo in every [TestMethod] (somewhat repetitive), I created a global TestFoo object in FooTests. If TestFoo fails to instantiate, I'm looking to fail all FooTests unit tests (as encoding/decoding will not work if the object fails to instantiate).

This isn't really a question of best practices, but I'm also curious as to whether or not this approach is terrible. I suppose another approach would be to set up an additional unit test to see whether or not TestFoo instantiates correctly.

Aucun commentaire:

Enregistrer un commentaire