Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I group unit tests using C# and NUnit?

I've a class with a lot of unit tests in C# (using NUnit 2.5.8) and I'd like to group the unit tests together based on which area of the class's functionality I'm testing (so I can quickly choose which set to run in the NUnit UI).

I know I could refactor the class into smaller components, which would solve the problem, but are there any other ways to do this without completely re-designing the production code?

like image 737
Jackson Pope Avatar asked Nov 03 '10 13:11

Jackson Pope


People also ask

What can be used for unit testing in C?

The most scalable way to write unit tests in C is using a unit testing framework, such as: CppUTest. Unity. Google Test.

Should each unit test be independent?

Good unit tests should be reproducible and independent from external factors such as the environment or running order. Fast. Developers write unit tests so they can repeatedly run them and check that no bugs have been introduced.

How complex should unit tests be?

The general rule for unit tests is to test the smallest possible piece that you can test. A good rule is that each test should exercise exactly a single method from a public API. That means it should only execute this method and no other, not even transiently.


1 Answers

Why not a [TestFixture] (separate class) for each area of functionality that you want to test?

class ClassThatDoesTooMuch {
    // functionality related to opening a database connection
    // functionality related to file management
    // functionality related to solving world hunger
}

[TestFixture]
public class ClassThatDoesTooMuchDatabaseConnectionTests { // }

[TestFixture]
public class ClassThatDoesTooMuchFileManagementTests { // }

[TestFixture]
public class ClassThatDoesTooMuchWorldHungerSolutionTests { // }
like image 149
jason Avatar answered Oct 15 '22 09:10

jason