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?
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):
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With