How to deliberately fail all tests in C #

Question:

I am trying to figure out if there is a way to compromise all C # unit tests whenever a particular condition is met.

BACKGROUND:

I set up unit test for an object that encodes and decodes its internal data. Here's a pretty far-fetched 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 data
    }

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

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

Instead of creating a new instance TestFooin each [TestMethod](somewhat repetitive) I created a global object TestFooin FooTests. If the TestFooinstance could not be created, I expect that all the tags will not be able to execute FooTests(since encoding / decoding will not work if the object is not created).

, , . , unit test, , TestFoo.

+4
2

#


TL; DR: ClassInit() , , Assert. - , .


, , - , ClassInitialize.

MSDN:

, , , - , , . .

... :

, , , . , TestInitializeAttribute.

Foo, Assert - null. , .

[ClassInitialize]
public void ClassInit(TestContext context)
{
    TestFoo = new Foo(dynamicDataFromContext);
    Assert.IsNotNull(TestFoo);
}

, , : Unit Test

, CRice Assert.Inconclusive(), .

+6

. Assert.Fail().

Assert.Inconclusive() , . , (), () - , , , , .

+2

Source: https://habr.com/ru/post/1653228/


All Articles