0

I am bit confused about the usage of setUp method in unittest

From the documentation

setUp()

Method called to prepare the test fixture. This is called immediately before calling the test method; other than AssertionError or SkipTest, any exception raised by this method will be considered an error rather than a test failure. The default implementation does nothing.

Now I understand that setUp would be called before each testcase from the testsuite.

But if each of the testcase needs to do something else in their respective setUp and tearDown, how can I do that in these methods. It seems they are generic for each of the testcases

Since each of the testcase is for the same feature so logically they belong to the same testsuite. I can easily write the setup and teardown steps in the test method itself, but then setUp and tearDown would lose its purpose

mittal
  • 915
  • 10
  • 29
  • Those methods are useful if you have operations to run before or after every tests, like open and close a file. You could also duplicate these operations right inside your tests, but that would be ugly. – olinox14 Jun 27 '19 at 15:07
  • Yes, but I may need to open/close different files before each of the testcases, right ? The same `setUp/tearDown` will not help – mittal Jun 27 '19 at 15:09
  • In this case you do this inside your tests. – olinox14 Jun 27 '19 at 15:09
  • Uggh !! That is exactly what I wish to avoid ;) in my original question – mittal Jun 27 '19 at 15:25
  • I understand, but it's better to add extra lines than to build something that would be too much complex... – olinox14 Jun 27 '19 at 15:30
  • The purpose of setUp and tearDown is to run *common code* before/after each tests within your class (eg open a connection to a database, or read some test data). Perhaps you can abstract out your setup/teardown requirements into different classes such that you keep it DRY, eg: one test class opens/closes one file. If this doesn't work for your requirements I'd suggest a helper function that you can call from within the test - that will improve both readability and reusability. – Richard Jun 27 '19 at 15:38
  • You won't be able to achieve test-specific setup with `unittest`, it's pretty limited. The only possibility coming to mind is to move each test that requires specific setup to a separate `TestCase` class. Ever thought about the change of test framework? Both `nose` and `pytest` have easy constructs (`with_setup` decorator in `nose`/fixtures in `pytest`) for test-specific setup and teardown. – hoefling Jun 27 '19 at 18:34
  • Aside from that, you can customize the code in `setUp`/`tearDown` by test name: `if self._testMethodName == 'testFeatureOne': ... elif self._testMethodName == 'testFeatureTwo': ...` etc, but it is ugly and can't compete with the excellent `pytest` fixtures IMO. – hoefling Jun 27 '19 at 18:54
  • And last but not least - what you are trying to do (separating the setup, teardown and the actual test) is the right thing to do; putting setup/teardown code in tests is a bad idea. A test should only fail from an assertion, not from the test preparation or the afterwards cleanup. – hoefling Jun 27 '19 at 19:03
  • "`setUp` would be called before each testcase from the testsuite." No, it will be called before each test in the `TestCase`. – Stop harming Monica Jul 01 '19 at 20:11

0 Answers0