Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to a run specific code before & after each unit test in Python

Following is the structure of my tests in a file.

Class
 setup
  test01
  test02
  test03
 teardown

I have a requirement to run specific code before and after each test.

For before, I could invoke that code from the setup. But for after the test, I am not able to figure how to do it. Obviously invoking the code from teardown would work for the last test, but how can I have it run for the tests in between?

like image 702
Anil Tallapragada Avatar asked Jun 05 '15 01:06

Anil Tallapragada


1 Answers

Assuming that you're properly using a class descended from unittest.TestCase, then the setUp method is run before each test, and the tearDown method is run after each test. Check the documentation. So it's completely feasible to put your code in those two methods.

like image 89
Daniel Martin Avatar answered Sep 28 '22 17:09

Daniel Martin