Question

Good afternoon/morning/evening folks,

I was wondering is it possible for me to "execute" a SpecFlow test via some sort of test harness (not NUnit)?

Previously my test harness I built ran MS Unit tests by calling methods from within the DLL that was created when I compiled the tests.

I'm assuming the same is possible in theory since a DLL is created, but im wondering how it will get all of the arguments etc.

So in short, is this possible if so is there a straight forward way to do this or am I barking up the wrong tree?

Was it helpful?

Solution 2

So I decided to invest some time on this and figured that using reflection was the way to do this task.

Here is some of my code:

TestRunner.TestDLLString = getDLL(project);
            var TestDLL = Assembly.LoadFrom(TestDLLString);

            Type myClassType = TestDLL.GetType("SeleniumDPS." + testname);


            var instance = Activator.CreateInstance(myClassType);

            MethodInfo myInitMethod = myClassType.GetMethod("Initialize");


            try
            {
                myInitMethod.Invoke(instance, null);
            }
            catch (Exception ex)
            {
//Error logging etc
            }

I then repeat that for the "[TestMethod]" etc. I understand some people dislike reflection but in this instance the performance isnt critical so it works quite well for us.

So essentially what im doing is reading on the name of the test from an XML file then searching the DLL for that test method, then executing the Intitialize method, and later on executing the test method itself. After the test is run I then execute the cleanup method.

It might seem a bit hacky and NUnit might seem the logical choice for some, but as I mentioned earlier I needed a customizable approach. Thanks for all the suggestions though.

OTHER TIPS

It's possible, but I'm not clear why you would want to.

Specflow is basically just a clever way of generating tests. Normally these are nUnit tests, but they can also be switched to use mstest. When you save your edits to the .feature file then VS runs a Custom Tool that converts your plaintext into a .feature.cs file that contains a code version of what you wrote with nUnit attributes applied to the methods.

Later, an nUnit runner (nUnit, resharper, gallio, teamcity etc) loads the dll and looks for all public methods marked with [Test] inside public classes marked with [TestFixture]. These methods get called.

There is nothing to stop you writing your own runner, however I'm not sure why you would do that. nUnit provides extensive reporting of the success of your test run in xml format, so its probably faster just to write something to parse that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top