GoodAtScrabble
GoodAtScrabble

Reputation: 51

Dynamically Generate Test Fixtures and Test Cases in NUnit using folder structures and XML Files

I am trying to generate tests dynamically based on the structure of folders that contain XML files. (The tests use the XML files to drive Selenium RC)

So say for example I have 3 Folders which also contain sub folders (each of which contain the Data.xml file I use to drive selenium RC)

TestData
    TestFixtureOne
        TestCaseOne
        Data.xml
        TestCaseTwo
        Data.xml
        TestCaseThree
        Data.xml    
    TestFixtureTwo
        TestCaseOne
        Data.xml
        TestCaseTwo
        Data.xml
    TestFixtureThree
        TestCaseOne
        Data.xml
        TestCaseTwo
            Data.xml
        TestCaseThree   
        Data.xml
        TestCaseFour
        Data.xml
        TestCaseFive
        Data.xml

The code I currently have is like so

using System;
using System.Text;
using System.Threading;
using NUnit.Framework;
using System.Collections.Generic;
using System.Collections;
using System.Configuration;
namespace NUnitIntegration
{
    [TestFixture]
    class SeleniumTest
    {
        private static string testDirectory;

        public SeleniumTest()
        {
            testDirectory = ConfigurationManager.AppSettings["TestDirectory"]; // Let’s assume this value is “C:\TestData\”

        }

        [SetUp]
        public void init()
        {
            // Nothing needed as of yet
        }

        [Test, TestCaseSource("GetTestCases")]
        public void TestSource(string test)
        {
            System.Console.WriteLine("Successfully executed test for: " + test);
        }

        [TearDown]
        public void dispose()
        {
            // Nothing needed as of yet
        }

        private static string[] GetTestCases()
        {
            return getTests();
        }

        private static string[] getTests()
        {
            return Directory.GetDirectories(testDirectory);
        }
    }
}

But this will only bring me back the TestFixtureOne, TestFixtureTwo, and TestFixtureThree folders (see below) which is not what I am after. I was trying to make it flexible so that I can added more tests if needed (more TestFixtures and TestCases).

[NUnit Test Tree] TestSource TestFixtureOne TestFixtureTwo TestFixtureThree

I have searched quite relentlessly recently and came across these threads which helped me very much, but now I am stuck!

used in multiple [TestFixture]s (And another thread where I got the above code from)

Any help would be greatly appreciated,

Regards

Upvotes: 2

Views: 2800

Answers (1)

David Blurton
David Blurton

Reputation: 150

I'm not sure whether you need to use the GetFiles() method to get the XML files, but to get the directories recursively you could use

return Directory.GetDirectories(testDirectory, "*", SearchOption.AllDirectories);

More information on this function here.

Upvotes: 1

Related Questions