1

I have some doubt with respect to addCleanup() --

  1. When addCleanup() will be executed, only when there is a failure in any steps or it will be executed in normal scenarios too (when there is no failure)?
  2. If there is some failure in test, tearDown() will be called but if some steps in tearDown() fails, can we add addCleanup() in tearDown() to call some function which can do a proper cleanup?
ash
  • 5,139
  • 2
  • 27
  • 39
DD1
  • 357
  • 4
  • 14
  • Where did you see this `addCleanup()` function? Are you writing tests? – Code-Apprentice May 09 '18 at 19:00
  • Yes, I am trying to add in my tests but not sure where/how to implement it, hence the question – DD1 May 09 '18 at 19:02
  • I have written a lot of python unit tests and have never seen `addCleanup()`. Where did you read about it or see it? – Code-Apprentice May 09 '18 at 19:02
  • https://docs.python.org/2/library/unittest.html#unittest.TestCase.addCleanup – JeffCharter May 09 '18 at 19:03
  • https://docs.python.org/2/library/unittest.html#unittest.TestCase.addCleanup – DD1 May 09 '18 at 19:03
  • According to [this answer](https://stackoverflow.com/a/43715888/1440565), you can call `addCleanup()` in `setUp()` or in a test method depending on your needs. – Code-Apprentice May 09 '18 at 19:05
  • `addCleanup` is executed at whatever point you execute it, which is up to you. Are you wondering when you're supposed to execute it, or when the cleanup function is executed, or something else? It's not clear what you're asking. – user2357112 May 09 '18 at 21:49
  • My question is when the cleanup function is executed, does it get executed only during a failure of it gets executed in a normal scenario(when there is no failure) as well? – DD1 May 10 '18 at 02:29

1 Answers1

0

Add a function to be called after tearDown() to cleanup resources used during the test. Functions will be called in reverse order to the order they are added (LIFO).

They are called with any arguments and keyword arguments passed into addCleanup() when they are added. If setUp() fails, meaning that tearDown() is not called, then any cleanup functions added will still be called.

addCleanup (function, *args, **kwargs)
Ankireddy
  • 179
  • 1
  • 6