2

TLDR; How can I create a pre-processing directive in C# based on values from an app.config file?

I am using Selenium to do integration testing. I have two test method classes which are responsible for testing different areas of my application. My test classes are identified as such with a [TestClass] attribute, and my test methods are identified using a [TestMethod] attribute like so:

[TestClass]
public class TestsForAreaX
{
    [TestMethod]
    public void TestFoo()
    {
        //Do test logic here
    }
    [TestMethod]
    public void TestBar()
    {
      //Do test logic here
    }
}

When I rebuild my solution all methods that have had the [TestMethod] applied will appear in the test explorer.

What I would now like to do is add some flags to my app.config to conditionally apply [TestMethod] attributes so that I can specify which tests I want run. The reason for this is that some tests are taking very long and I would like to exclude them without having to fiddle with method attributes directly.

I am aware of pre-processing directives like:

#if DEBUG
    //debug mode
#elif
    //release mode
#endif

but that only works for build configurations and not values in app config files, unless I am mistaken?

1 Answer 1

2

You cannot remove the attributes of the assembly after compilation. Maybe you are looking for Categories:

[TestClass]
public class TestsForAreaX
{
    [TestCategory("LongRunning"), TestMethod]
    public void TestFoo()
    {
        //Do test logic here
    }
    [TestCategory("ShortRunning"), TestMethod]
    public void TestBar()
    {
      //Do test logic here
    }
} enter code here

Then you can run tests for different categories at least from the command line, this site explains how: http://msdn.microsoft.com/en-us/library/dd286683.aspx

1
  • This is exactly the kind of thing that I was looking for. I have no problem with using a command line either. Ty.
    – GoofyBall
    Feb 26, 2014 at 13:07

Your Answer

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

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