Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I have code that executes before and after tests are run by NUnit?

Tags:

c#

nunit

I've got a bunch of tests in NUnit which create garbage data on the filesystem (bad, I know, but I have little control over this). Currently we have a cleanup tool that removes these temporaries and such, but I'd like to be able to run that cleanup tool automatically. I'd have to be able to run it after all tests have finished running. I have similar checking that I'd like to do at the beginning, to ensure that there are none of these temporaries left from previous runs that might change the outcome of the tests.

Is such a thing simple or am I going to have to implement a whole new test runner for such a thing?

like image 871
Billy ONeal Avatar asked Jun 07 '10 20:06

Billy ONeal


People also ask

Which attribute in NUnit lets you execute a method before and after a test case?

This attribute is to identify methods that are called once prior to executing any of the tests in a fixture.

Does SetUp run before every test NUnit?

Inheritance. The SetUp attribute is inherited from any base class. Therefore, if a base class has defined a SetUp method, that method will be called before each test method in the derived class.

Which of the following attributes will allow to execute a method before and after each test is executed?

Action Attributes allow the user to create custom attributes to encapsulate specific actions for use before or after any test is run.

Is NUnit a test runner?

The nunit.exe program is a graphical runner. It shows the tests in an explorer-like browser window and provides a visual indication of the success or failure of the tests.


Video Answer


2 Answers

Yes,

Use the [SetUpFixture] attribute on a class and the [SetUp] and [TearDown] attributes on methods with that class.

The SetUp method in a SetUpFixture is executed once before any of the fixtures contained in its namespace. The TearDown method is executed once after all the fixtures have completed execution. In the examples below, the method RunBeforeAnyTests() is called before any tests or setup methods in the NUnit.Tests namespace. The method RunAfterAnyTests() is called after all the tests in the namespace as well as their individual or fixture teardowns have completed exection.

Source (it says 2.4 on the page, but it is available in 2.5)

like image 134
ChrisF Avatar answered Oct 10 '22 19:10

ChrisF


Take a look at TestFixtureSetUp and TestFixtureTearDown.

like image 39
ThatBlairGuy Avatar answered Oct 10 '22 19:10

ThatBlairGuy