Calum
Calum

Reputation: 2130

C# test dependancies

I am very aware and agree with the opinion that tests should not be dependant on one another.

However in this instance I feel it would be beneficial.

The situation is the system under test has a step by step process that needs to be followed (there is no way to jump to a step without going through the previous ones). In the ideal world we would get the devs to add an API to allow us to do thus, but given the constraints this will not be done.

Currently the tests being done are all end to end making failing tests difficult to analyse at times.

My question is then: Is there a clean way I can break down these end to end tests into smaller tests, and impose some dependencies on them?

Im aware that TestNG can do this using the @DependOn notation, is there a similar concept for C#?

Upvotes: 0

Views: 123

Answers (3)

Calum
Calum

Reputation: 2130

I agree with almost all the comments to date, the solution I ended up going with be it not the cleanest or most 'SOLID', was to use nUnit as the framework, using the category attribute to order the tests.

Upvotes: 0

jamarchist
jamarchist

Reputation: 46

A couple of concepts might be useful here:

The basic idea here is to separate the scenario you are executing from the assertion you are making (i.e. what you are testing). This is really just BDD, so it may make sense to use a BDD Framework to help you, although it's not necessary. With NUnit, you might write something like this

[TestFixture]
public class ExampleTests
{
    [SetUp]
    public void ExecuteScenario()
    {
        GrabBucket();
        AddApple();
        AddOrange();
        AddBanana();
    }

    [Test]
    public void ThereShouldBeOneApple()
    {
        Assert.AreEqual(1, Count("Apple"));
    }

    [Test]
    public void ThereShouldBeOneOrange()
    {
        Assert.AreEqual(1, "Orange");
    }

    [Test]
    public void ThereShouldBeOneBanana()
    {
        Assert.AreEqual(1, "Banana");
    }

    [Test]
    public void ThereShouldBeNoPomegranates()
    {
        Assert.AreEqual(0, "Pomegranate");
    }

    private void GrabBucket() { /* do stuff */ }
    private void AddApple() { /* do stuff */ }
    private void AddOrange() { /* do stuff */ }
    private void AddBanana() { /* do stuff */ }

    private int Count(string fruitType)
    {
        // Query the application state
        return 0;
    }
}

I realize this doesn't answer your question as stated - this isn't breaking the larger integration test down into smaller units - but it may help you solve the dependency problem; here, all the related tests are depending on execution of a single scenario, not of previously-executed tests.

Upvotes: 0

David Arno
David Arno

Reputation: 43264

From the way you describe your tests, either:

  1. The way the developers have developed the code is flawed, in that to test step 3 say, steps 1 and 2 must be invoked first, rather than being able to isolate step 3. If the problem is with the way the developers designed the system, suggest they fix it.
  2. You are performing integration tests and want to test the results of invoking several steps. In that case, you do not want to use a unit test tool, you need an integration test tool. See this answer to another question for advice on such tools and their pitfalls.

Upvotes: 1

Related Questions