Attributes in Nunit

What is attribute?

Attributes are used to identify tests in nunit.

Apartment attribute

This attribute is used to specify in which apartment the test should run. There are two kinds of apartment.

  • Single Thread Apartment
  • Multi Thread Apartment

The attribute is used on test, test fixtures or assembly.

Author attribute

Author attribute is used to add information about test author and e-mail address(optional). It can be included in test and test fixture.

Category attribute

Category attribute is used to specify group for tests. Only the tests under the particular category will run when it is run by category. This attribute can be used both in test fixture and test.

Combinatorial attribute

Combinatorial attribute is used to generate all possible combinations of test cases for the individual parameters declared within the test. In the above example, 9 tests are generated.

Culture attribute

This attribute specifies the NUnit on which culture the test or test fixture should run.

Datapoint attribute

This attribute is used to provide data for theory. In the above example tests are categorized based on the data type specified in test fixture. The values for the corresponding data type is specified under datapoint.

Datapoint Source attribute

This attribute is used to provide data for theory. In the above code, a test is created for each value in the datapointsource.

DefaultFloatingPointTolerance attribute

This attribute is used to provide tolerance for the comparison. From the above code, ToleranceDemo1 uses tolerance declared under TestFixture while ToleranceDemo2 uses tolerance declared under Test. The test ToleranceDemo3 got failed as 2.6 is out of tolerance limit.

Description attribute

This attribute is used to provide description for assembly, test or test fixture.

Explicit attribute

This attribute marks the assembly, test fixture or test to run only when explicitly selected for run. In normal cases, the test will skipped. In the above example, Demo1 is skipped as Explicit is mentioned in Test whereas Demo2 ran successfully.

Ignore attribute

Ignore attribute is used to remove the test or test fixture from execution for some reason. In the above code, we have used ignore attribute in Test1. During execution, Test1 is skipped whereas Test got executed successfully.

LevelOfParallelism attribute

LevelOfParallelism is used to specify number of threads for running the tests. By default it chooses processor count of the machine.

MaxTime attribute

MaxTime attribute is used to specify maximum time a test can wait to complete. The time is mentioned in milliseconds. If a test takes more than the mentioned time, it will fail.

NonParallelizable attribute

NonParallelizable attribute is used to indicate nunit that the particular test, test fixture or assembly cannot run in parallel execution. In the above example, Test1 will not run in parallel execution.

OneTimeSetUp attribute

OneTimeSetUp is used within the test fixture and will be executed before a test runs. If it throws an error, none of the tests will get executed. In the above code, the method defined under the OneTimeSetUp will run first followed by Test.

OneTimeTearDown attribute

OneTimeTearDown is used within the test fixture and will be initiated after a execution of tests. In the above code, the method defined under the OneTimeTearDown will run last.

Order attribute

Order attribute is used to specify the order in which the tests should run. In the above code, Test runs in following order :- TestA, TestD, TestB and TestC.

Pairwise attribute

Pairwise attribute is used to generate all possible pairs of the input given. In the above code, 8 such combinations of tests are created.

Parallelizable attribute

Parallelizable attribute is used to specify the nunit that the tests may run in parallel. No parallel execution takes place by default. This attribute can be declared in test, test fixture and assembly.

Platform attribute

This attribute specifies the platform on which the test should run. In the above code, 32 bit platform is excluded in test fixture and Test 2 is specified to run only in Windows XP. Following are some of the platform specifiers : - Windows7, Win2012Server, Windows8, Unix, Linux, NetCF, SSCLI, Rotor, Mono, 64-Bit etc...

Property attribute

This attribute is used to specify property for a test or test fixture.

Random attribute

This attribute is used to send random values as input for the method. In Test1, Random(Count) attribute is used, where 5 (count) random numbers are generated without any conditions. In Test2, Random(min,max,count) attribute is used, where 3 random numbers between 20(min) and 50(max) are generated.

Range attribute

This attribute is used to mention range of values to be send as parameters in a method. In the above example, RangeAttribute(From,To) is used as parameters in Test1. So all the values between 1(From) to 4(To) are passed here. In Test2, RangeAttribute(From,To, Step) is used, where the the from values are generated between 1(From) to 4(To) in increment of 2(Step).

Repeat attribute

This attribute is used to run particular test multiple times. In the above example, Test 1 runs repeatedly for 5 times.

RequiresThread attribute

This attribute is used to specify that the assembly, test fixture or test requires separate thread to run.

Retry attribute

Retry attribute is used to rerun the particular test for specific count till it gets passed. In the above example, Test 1 retries for maximum of 5 times, till it gets passed.

Sequential attribute

Sequential attribute is used to generate test cases from individual data. Additional combinations are not generated in this case. In the above example only two combinations are generated.

SetCulture attribute

SetCulture attribute is used to specify the current culture for the duration of the test. After the test it will return to its original value.

SetUICulture attribute

SetUICulture attribute is used to specify the current UI culture for the duration of the test. After the test it will return to its original value.

SetUp attribute

SetUp attribute is used to run a particular set of methods before running each test in the test fixture.

SingleThreaded attribute

SingleThreaded attribute indicates the tests including setup and tear down should run only on the same thread. This is defined under TestFixture.

TearDown attribute

Some common sets of methods which need to be performed after every test method are declared under tear down attribute.

Test attribute

A test attribute is used to indicate a method as test in test fixture. In the above example, Test1 and Test2 are different tests available for execution.

TestCase attribute

A test case attribute is used to indicate a method with parameter as a test and also to pass data for the method. In the above example, TestCase attribute is passing three different sets of values in Test1. In Test2, the return value data and value in the expected result are compared. Whereas in Test1 it is compared using Assert.AreEqual.

TestCaseSource attribute

This attribute indicates the source from which required data will be provided for the test case.

TestFixture attribute

TestFixture attribute indicates the class with tests including setup and teardown methods.

TestFixtureSource attribute

TestFixtureSource attribute indicates the source from which data will be provided for the test fixture.

TestOf attribute

The information about the class being tested is indicated in TestOf attribute. It is specified on test or test fixture.

Theory attribute

Theory attribute is a special kind of test in which developer passes certain within the code. Datapoint Attribute or DatapointSource Attribute are the source of data for theory.

Timeout attribute

A test is failed if it crosses the time mentioned in the timeout attribute. The time is declared in milliseconds.

Values attribute

Values attribute is used to provide set of data as input for parameter of tests.

ValueSource attribute

ValueSource attribute is used to provide source of data as input for parameter of tests.

Leave a Reply

Your email address will not be published. Required fields are marked *