Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting nUnit selected categories programmatically

Tags:

c#

nunit

Is there a way of programatically getting the selected test categories while executing a test? something in the lines of TestContext.Properties["_SELECTCATEGORIES"]

basically i've got test cases which load the test data from a db and as i've got a lot of tests the project is taking a long time to load. Im trying to find a way of having the testCaseSources returning nothing if the category is not selected

like image 527
z0c Avatar asked Jun 13 '12 15:06

z0c


1 Answers

UPDATED

There does not appear to be any straightforward method for identifying or loading selected categories in an NUnit test assembly within the NUnit Framework itself..

Using reflection, you could perhaps scan the property Categories in classes decorated with TestAttribute or TestFixtureAttribute. By matching these categories with the one(s) you want to load, you could be able to filter out which tests to load before loading.

And then there is the TestContext.Test.Properties key _CATEGORIES (available in NUnit 2.5.7 and later):

[Test]
[Category("Hello")]
public void TestCategory()
{
  Assert.IsTrue(((ArrayList)TestContext.CurrentContext.Test.Properties["_CATEGORIES"]).Contains("Hello"));
}

Some more to read on the TestContext class can be found here. Of course, to solve the problem with this approach would require you to load the entire test assembly and loop over all test cases in beforehand, which is obviously undesirable in your scenario.

like image 150
Anders Gustafsson Avatar answered Oct 18 '22 05:10

Anders Gustafsson