Executing NUnit tests through a Windows Form application with NUnit Engine

630 views Asked by At

I'm trying to execute NUnit tests through a Windows Form application with NUnit Engine, but I don't understand how to set the path for the DLL where my tests are (I have already included the DLL in the references). When I click a button, I want the tests to start; however, NUnit opens and then immediately closes without doing anything. Here's what I have:

namespace ATF.GUI
{
    public partial class ATF_Main : Form
    {
        TestPackage package;
        ITestEngine engine;

        public ATF_Main()
        {
            InitializeComponent();
        }

        private void ATF_Main_Load(object sender, EventArgs e)
        {
            string path = Assembly.GetExecutingAssembly().Location;
            package = new TestPackage(path);
            package.AddSetting("Working Directory", Environment.CurrentDirectory);

            // Prepare the engine
            engine = TestEngineActivator.CreateInstance();
        }

        private void btnStartTests_Click(object sender, EventArgs e)
        {
            using (ITestRunner runner = engine.GetRunner(package))
            {
                // Execute the tests 
                XmlNode result = runner.Run(null, TestFilter.Empty);
            }
        }
2

There are 2 answers

0
Mike On BEST ANSWER

I never got a real answer to this but I figured it out myself.

private void ATF_Main_Load(object sender, EventArgs e)
    {
        // Add reference to tests DLL and load it here by name
        Assembly testAssembly = Assembly.Load("Program.Tests");
        package = new TestPackage(testAssembly.Location);
        package.AddSetting("Working Directory", Environment.CurrentDirectory);
    }

You could also add the assembly locations to a List if you have multiple test assemblies.

7
Charlie On

You are setting the test assembly to be your gui assembly. Since it has no tests, NUnit finds nothing to do. I imagine it returns an error in the result.

Somehow, your application has to be given the path of the test assembly. This can be through a command-line or a dialog of some kind. You can look at the code for nunit3-console or nunit-gui to see how it is done.

Your idea of using a referenced assembly seems a bit odd for a packaged application. Your users will need to have the source and rebuild it each time, referencing the desired test assembly. Do you really want that?

In case you do, you will need to find some way to get at that reference. Hard to do if there is nothing constant that will always be in it.