Home > database >  Grouping Unity NUnit tests?
Grouping Unity NUnit tests?

Time:10-25

I want to group tests like this:

enter image description here

To get this screenshot I used TestCases, but they don't really apply to my needs.

Obviously I don't want this:

[TestCase(TestName = "ShouldDoSomething")]
[TestCase(TestName = "ShouldDoSomethingElse")]
public void OnJoinRoom()
{ ... }

I want something like this:

[TestGroup("OnJoinRoom")]
public void ShouldDoSomething()
{ ... } 

[TestGroup("OnJoinRoom")]
public void ShouldDoSomethingElse()
{ ... } 

I don't think using subclasses is an option, as the tests are depending on a SetUp method used in the TestFixture class.

CodePudding user response:

In NUnit, TestFixtures are used to group tests as well as to provide a common setup. The normal way to provide a common setup across multiple TestFixtures is to have a common base class like

public class CommonBase
{
    [SetUp]
    public void CommonSetUp() { }
}

[TestFixture]
public class OnJoinRoom : CommonBase { }

[TestFixture]
public class OnLeaveRoom : CommonBase { }

Notes:

  1. Put all your tests in the derived classes
  2. TestFixture attribute is optional, of course, but do not put it on the base class.
  3. Base class may be abstract if you like.
  • Related