7

Imagine I have tests like that:

import unittest

class MyTests(unittest.TestCase):

  print("Starting")

  def test_first(self):
    .....

Is the print statement guaranteed to be executed before test_first() and the rest? From what I've seen it does get executed first, but are there any edge cases?

1 Answer 1

8

You can use setUp()(docs) and setUpClass()(docs) methods for this. The setUp() method is executed before each individual test while the setUpClass() method is executed before all the tests in this class run.

import unittest

class MyTests(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        print("Starting all the tests.")

    def setUp(self):
        print("Starting another test.")

    def test_first(self):
        ...

Not the answer you're looking for? Browse other questions tagged or ask your own question.