2

Typically, (unit) tests should be independent and work on their own, however, in system testing we face different requirements. For example, I am implementing a test for a setup. Standard functionality is (1) install, (2) repair and (3) uninstall. Obviously, repair and uninstall cannot be run independently of install. Therefore, NUnits Order attribute comes in handy to run install first, followed by repair and then uninstall.

The setup creates a log file for each step and I test that log for different error and/or success messages. I implemented a data driven test which I would like to call after each functionality. Since the other tests have to run in a specific order, I cannot call the test multiple times.

The only solution I have is to take the logic of that test into a class and call that class at the end of each test. This, however, does not allow for a data driven test case that appears in the results. I am wondering if there is any smarter solution to avoid my current code duplication?

Some pseudo code illustrating my issue:

[TestFixture, Category("Setup"), Order(1)]
public class InstallTest
{
    [Order(1), Test]
    public void Install()
    {
        //Install
    }

    //Duplicated log file test code 
    [Order(2)]
    [TestCase("...")]
    [TestCase("...")]
    public void CheckLogFile()
    {
        //log file test code 
    }
}

[TestFixture, Category("Setup"), Order(2)]
public class Repair
{
    [Order(1), Test]
    public void Uninstall()
    {
        //Uninstall
    }

    //Duplicated log file test code 
    [Order(2)]
    [TestCase("...")]
    [TestCase("...")]
    public void CheckLogFile()
    {
        //log file test code 
    }
}

[TestFixture, Category("Setup"), Order(3)]
public class UninstallTest
{
    [Order(1), Test]
    public void Uninstall()
    {
        //Uninstall
    }

    //Duplicated log file test code 
    [Order(2)]
    [TestCase("...")]
    [TestCase("...")]
    public void CheckLogFile()
    {
        //log file test code 
    }
}
0

Refactoring

The longer explanation:

You extract the log file test code from the unit tests and your system tests and place it into a separate class that's accessible to both. If you need to add parameters, you add them so that instead of three near-identical log test methods, you have three log test methods that call the common method.

You may also include in your setup a helper to clear the log file if you prefer to work that way.

The end result would be something like this:

[TestFixture, Category("Setup"), Order(1)]
public class InstallTest
{
    // other code snipped for brevity
    public void CheckLogFile()
    {
        // if anything needs to be passed in set it up here
        string param1 = "something";
        LogFileChecker(param1);
    }
}

[TestFixture, Category("Setup"), Order(2)]
public class Repair
{
    // other code snipped for brevity
    public void CheckLogFile()
    {
        string param1 = "Repair test case";
        int startLine = 256;
        LogFileChecker(param1, startLine)
    }
}

... and so on. Your actual log file check code would include the parameters that you need to send. You could also adjust it to return the results of the check even though that would add some duplication, simply to make it clear that your assertions are happening in your test routines and not the helper code.

  • That's more or less what I try to avoid. Your code would still have the same duplication like mine (unless you move the params into LogFileChecker, but then they won't appear in any summary). I guess I could move the params to a params test class, but it would be so much easier, if I could just call the test including the parameters multiple times. This code I have copied in each TestFixture: [Order(2)] [TestCase("...")] [TestCase("...")] public void CheckLogFile(string param) { LogFileChecker(param) } – tomwaitforitmy Jan 10 at 14:03
  • If the parameters are the same, you can set them up as constants within the test class. I prefer to name constants so it's obvious what they are: START_AT_LINE_NUMBER and so forth. – Kate Paulk Jan 10 at 14:15
  • Yes, but they still won't appear in my results. It appears I am looking for depended tests like this: stackoverflow.com/a/1781858/7559642 – tomwaitforitmy Jan 10 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.