Last active
February 11, 2017 00:32
-
-
Save jorotenev/922a0ca6071a94f495ab0fa2c5222647 to your computer and use it in GitHub Desktop.
Example of using mixins to make python tests more flexible. Particular interest in order of execution of setUp and setUpClass
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
from unittest import TestCase | |
""" | |
Showcase how to use mixins and multiple inheritance to write tests | |
""" | |
class BaseTest(TestCase): | |
""" | |
A base class to be inheritated by actuall test classes. | |
""" | |
def setUp(self): # 3 | |
print("BaseTest:setUp called") | |
self.boo = "gladen sum" | |
@classmethod | |
def setUpClass(cls): # 1 | |
print("BaseTest::setUpClass called") | |
cls.browser = 'musaka' | |
class FullDBMixin(object): | |
def setUp(self): # 5 | |
super(FullDBMixin, self).setUp() | |
print("FullDBMixin::setUp called with instance attribute [boo] = %s" % self.boo) | |
class LoginMixin(object): | |
@classmethod | |
def setUpClass(cls): # 2 | |
super(LoginMixin, cls).setUpClass() | |
print("LoginMixin::setUpClass called") | |
def setUp(self): # 4 | |
super(LoginMixin, self).setUp() | |
print("LoginMixin::setUp called") | |
self.login() | |
def login(self): | |
print("LoginMixin::login called with class attribute [browser] %s" % self.browser) | |
# order of inheritance **matters** | |
class TestAuthontecation(LoginMixin, FullDBMixin , BaseTest): | |
def test_user_dashboard(self): | |
# test stuff without needing to setup the db or login the user | |
pass | |
if __name__ == '__main__': | |
unittest.main() | |
# georgi@georgi-laptop:~$ python test.py | |
# BaseTest::setUpClass called | |
# LoginMixin::setUpClass called | |
# BaseTest:setUp called | |
# FullDBMixin::setUp called with instance attribute [boo] = gladen sum | |
# LoginMixin::setUp called | |
# LoginMixin::login called with class attribute [browser] musaka | |
# . | |
# ---------------------------------------------------------------------- | |
# Ran 1 test in 0.000s | |
# OK |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you elaborate on the order as in line 43?