0

I am working with teamcity and and moving some old integration tests over to it. These old integration tests just logged the results as a console application. I would stay with just logging the results but, teamcity has nice support for NUnit and would like to take advantage of it.

Lets say this was one of my old tests

    public void Test ()
    {
        int tries = 10;
        int fails = Check(tries);

        Console.WriteLine("{0} fails out of {1} attempts", fails, tries); // log results
    }

    public int Check (int tries)
    {
        int fails = 0;
        for (int i = 0; i < tries; i++)
        {
            if (i % 2 == 1) fails++;
        }
        return fails;
    }

I am not sure how to do this with NUnit or if it's even possible. I want each call of Check to be it's own test. And no, blocking out 10 tests to call Check (n) n = 0, ... n = 9 is not what I'm looking for.

Thanks in advance.

2 Answers 2

2

You can make use of NUnit's data-driven tests.

 [TestCase(10, Result = 5)]
 public int Check (int tries)
    {
    int fails = 0;
    for (int i = 0; i < tries; i++)
    {
        if (i % 2 == 1) fails++;
    }
    return fails;
 }
Sign up to request clarification or add additional context in comments.

Comments

0

I ended up going with the range attribute

[Test]
public int Check ([Range(0,100,1)] int tries)
{
   for (int i = 0; i < tries; i++)
   {
       if (i % 2 == 1) Assert.Fail("failed on :" + i);
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.