Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python unittests, statement before test cases

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?

like image 331
akalikin Avatar asked Aug 20 '15 08:08

akalikin


1 Answers

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):
        ...
like image 103
geckon Avatar answered Sep 30 '22 22:09

geckon