Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global setup and teardown blocks in Test::Unit

Tags:

What's the best way to have a setup run before every method in an entire test suite (not just one test class)?

Rspec allows you to define global before and after blocks. Is there a clean comparable way to do this in Test::Unit that doesn't involve mixing a module into each test class?

like image 622
samg Avatar asked Nov 15 '09 04:11

samg


People also ask

What is setUp and tearDown in testing?

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?

Prepare and Tear Down State for a Test Class XCTest 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 JUnit?

JUnit creates all the TestCase instances up front, and then for each instance, calls setup(), the test method, and tearDown(). In other words, the subtle difference is that constructors are all invoked in batch up front, whereas the setUp() method is called right before each test method.

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

Assuming you're using Rails. Just add following in your test/test_helper.rb file.

class ActiveSupport::TestCase   setup :global_setup    def global_setup     #stuff to run before _every_ test.   end end 

Tested on Rails 3.0.9.

like image 82
Vikrant Chaudhary Avatar answered Sep 28 '22 08:09

Vikrant Chaudhary