0

I'm trying to move my unittest tests into pytest so I can easily parameterise them. The setUp function for one of the classes is:

def setUp(self):
    self.engine.execute(f'DROP DATABASE IF EXISTS {db1}')
    self.engine.execute(f'DROP DATABASE IF EXISTS {db2}')

What is the best way to replicate this in pytest? I realise there is setup_method() however I was cheating a little with unittest as not every method in that class needed that particular setup.

pytest recommend using fixtures but all the examples I see are returning objects, not executing functions. Can/should fixtures be used in this way?

Jossy
  • 589
  • 2
  • 12
  • 36
  • Yes, you don't need to return anything from the fixture. Usually setup/teardown is done in a fixture with the part before `yield` executed before the test (setup), and the part after the `yield` after the test (teardown). – MrBean Bremen Jul 22 '20 at 18:50
  • Your first comment answered my question :-) Happy to accept that as an answer? – Jossy Jul 22 '20 at 18:58
  • Well, as this already has been answered before, it is better to flag this as a duplicate instead of repeating that answer - but thanks anyway :) – MrBean Bremen Jul 22 '20 at 19:01
  • Ah well I see my question as being different. I'd read through all the docs on fixtures so understood the logic of using them to setup and teardown objects. I just wasn't sure on using them to solely execute functions! – Jossy Jul 22 '20 at 19:26
  • Actually, [this answer](https://stackoverflow.com/a/39401087/12480730) is exactly what I tried to describe. – MrBean Bremen Jul 22 '20 at 19:53

0 Answers0