NUnit Categories in combination?

假装没事ソ 提交于 2019-12-20 11:20:32

问题


In my NUnit testfixtrues i have something along the lines of

[Test,Category("catA")]
public void test1
{
    //
}

[Test,Category("catB")]
public void test2
{
    //
}

[Test,Category("catA")]
[Test,Category("catB")]
public void test3
{
    //
}

Now in the NUnit gui i want to be able to select catA and catB and run the tests where catA and catB are present. Currently this is not the case and NUnit will run all 3 tests.

Is there any way to change this behavior to an AND condition rather than OR?

I'm currently running v2.5.0.9122.

Thanks in advance.


回答1:


The quick answer from 2019 for NUnit 3.0. It's possible to use multiple categories.

Consider the following example:


    [TestFixture]
    public class Tests1
    {
        [Test]
        [Category("A")]
        public void TestA()
        {
            Console.WriteLine(TestContext.CurrentContext.Test.Name);
        }

        [Test]
        [Category("B")]
        public void TestB()
        {
            Console.WriteLine(TestContext.CurrentContext.Test.Name);
        }

        [Test]
        [Category("C")]
        public void TestC()
        {
            Console.WriteLine(TestContext.CurrentContext.Test.Name);
        }

        [Test]
        [Category("A")]
        [Category("B")]
        public void TestAB()
        {
            Console.WriteLine(TestContext.CurrentContext.Test.Name);
        }

        [Test]
        [Category("A")]
        [Category("C")]
        public void TestAC()
        {
            Console.WriteLine(TestContext.CurrentContext.Test.Name);
        }

        [Test]
        [Category("B")]
        [Category("C")]
        public void TestBC()
        {
            Console.WriteLine(TestContext.CurrentContext.Test.Name);
        }

        [Test]
        [Category("A")]
        [Category("B")]
        [Category("C")]
        public void TestABC()
        {
            Console.WriteLine(TestContext.CurrentContext.Test.Name);
        }
    }

You can run the tests with both categories A and B and excluding category C using this command argument: --where="cat==A && cat==B && cat!=C"

Or you can run the tests with any of categories A and B and excluding C like this: --where="(cat==A || cat==B) && cat!=C"




回答2:


based on the docs, you just say /include:CatA+CatB

http://www.nunit.org/index.php?p=consoleCommandLine&r=2.5.1

Specifying Test Categories to Include or Exclude

NUnit provides CategoryAttribute for use in marking tests as belonging to one or more categories. Categories may be included or excluded in a test run using the /include and /exclude options. The following command runs only the tests in the BaseLine category:

nunit-console myassembly.dll /include:BaseLine The following command runs all tests except those in the Database category:

nunit-console myassembly.dll /exclude:Database Multiple categories may be specified on either option, by using commas to separate them.

Notes: Beginning with NUnit 2.4, the /include and /exclude options may be combined on the command line. When both are used, all tests with the included categories are run except for those with the excluded categories.

Beginning with NUnit 2.4.6, you may use a Category Expression with either of these options:

  • A|B|C Selects tests having any of the categories A, B or C.
  • A,B,C Selects tests having any of the categories A, B or C.
  • A+B+C Selects only tests having all three of the categories assigned
  • A+B|C Selects tests with both A and B OR with category C.
  • A+B-C Selects tests with both A and B but not C.
  • -A Selects tests not having category A assigned
  • A+(B|C) Selects tests having both category A and either of B or C The comma operator is equivalent to | but has a higher precendence. Order of evaluation is as follows:

    Unary exclusion operator (-) High-precendence union operator (,) Intersection and set subtraction operators (+ and binary -) Low-precedence union operator (|) Note: Because the operator characters have special meaning, you should avoid creating a category that uses any of them in it's name. For example, the category "db-tests" could not be used on the command line, since it appears to means "run category db, except for category tests." The same limitation applies to characters that have special meaning for the shell you are using.




回答3:


No. There is no way to only run tests that belong to two, or more, specific categories. To be honest when we first put the feature in several years ago I never thought of that. We tried to keep it as simple as possible.

By the way, you don't need to specify [Test] twice on your test3 method.

[Test]
[Category("catA")]
[Category("catB")]
public void test3
{
    //
}

Not that it makes a difference. It's just a style preference.




回答4:


If you use version 3.0 use the option --where. Example:

nunit3-console.exe youdll.dll --where="cat==yourCat"



回答5:


Sounds like what you need is a third category of "catAandB".




回答6:


As far as I know you can't chose both of them as NUnit stands.

I tried a number of different things with NUnit and the way that my tests were created with no success.

I have found a site that talks you through the process of creating custom category attributes but still can't see how that may help.




回答7:


"...nunit-console.exe" "....myassembly.dll" /include:catA+catB


来源:https://stackoverflow.com/questions/1199611/nunit-categories-in-combination

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!