简体   繁体   中英

c# and Nunit One time setup with multiple categories

it is possible set OneTimeSetUp with multiple categories?

[OneTimeSetUp, Category("cat1"), Category("cat2")]
public void Setup()
{
    var currentCategory = // get the current category i set in visual studio Trait:"" for example
    // do stuff
}

the main purpose that i need to run different one time setup for each category.

thanks!

As stated in the comment, it's not possible.

Categories are simply tags placed on a test that allow you to select tests for execution. Once execution begins, it follows the hierarchy that you built into your tests when you designed them.

In other words, you should organize your tests into test fixture classes primarily on the basis of the setup that each test needs. If any test in a fixture does not need the SetUp or OneTimeSetUp of that fixture, then it doesn't belong in that fixture. This, BTW, is not simply a matter of opinion, it's how NUnit is designed. If your test design doesn't mesh with NUnit's design, then your tests will do it's bets to run them but you won't get full benefit from it.

Those test classes (fixtures) should also be organized in namespaces according to the needs you have for OneTimeSetUp. You can only share OneTimeSetUp across classes if they are in the same namespace.

Hope this answer helps, even though it's "No!"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM