Getting nUnit of selected categories programmatically

Is there a way to programmatically retrieve selected test categories when running a test? something in the lines of TestContext.Properties ["_ SELECTCATEGORIES"]

Basically, I have test cases that load test data from db, and since I have many tests that the project takes a long time to load. I am trying to find a way when testCaseSources returns nothing if no category is selected

+6
source share
1 answer

UPDATED

There is no simple method for identifying or loading selected categories in a NUnit test assembly in the NUnit framework itself.

Using reflection, you can scan the Categories property in classes decorated with TestAttribute or TestFixtureAttribute . By comparing these categories with the ones you want to download, you can filter which tests to load before downloading.

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

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

Read on to the TestContext class here . Of course, to solve the problem with this approach, you will need to preload the entire test assembly and cycle through all the test cases, which is clearly undesirable in your scenario.

+2
source

Source: https://habr.com/ru/post/917987/


All Articles