Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a base class for unit test setup and teardown

Assuming Visual Studio.NET 2008 and MsTest are used to run unit tests. When a system is based on a service container and dependency injection unit testing would naturally require the setting up of services.

Should these services rather be setup in a base class that all unit tests derive from or in each unit test class. If they should be in a base class, is there a way to have the TestInitialize and ClassInitialize methods be executed without requiring them to be called from the derived class, e.g base.Initialise?

like image 251
sduplooy Avatar asked Jan 27 '09 14:01

sduplooy


People also ask

How do we run setUp () and tearDown ()?

Prepare and Tear Down State for a Test ClassXCTest runs setUp() once before the test class begins. If you need to clean up temporary files or capture any data that you want to analyze after the test class is complete, use the tearDown() class method on XCTestCase .

What is setUp and tearDown in unit test?

setUp() — This method is called before the invocation of each test method in the given class. tearDown() — This method is called after the invocation of each test method in given class.

Where will you use setUp () and tearDown () methods and make it run once for all the test in the class?

As outlined in Recipe 4.6, JUnit calls setUp( ) before each test, and tearDown( ) after each test. In some cases you might want to call a special setup method once before a series of tests, and then call a teardown method once after all tests are complete.

What is tearDown in testing?

A teardown test case will execute at the end of your test run within a test folder. Teardown test cases are used to perform post test execution actions. For example, a teardown test case can be used to delete test data generated during test execution.


1 Answers

With 2008, you should be able to have [TestInitialize] on a base class, and as long as you don't add another [TestInitialize] somewhere down the hierarchy, it should be called. You could also do things with virtual methods.

like image 164
Dominic Hopton Avatar answered Oct 12 '22 23:10

Dominic Hopton