Created
August 11, 2017 13:45
-
-
Save arsenlosenko/f12e59830aa4febf77c8a1c009ab8840 to your computer and use it in GitHub Desktop.
Unusual fixes or usecase for python unittest library
This file contains hidden or 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
#!/usr/bin/env python3 | |
import unittest | |
""" | |
Example test for running similar test | |
with parameters in a loop | |
""" | |
class SimilarTestsRun(unittest.TestCase): | |
testsmap = { | |
'setA': [1, 1], | |
'setB': [1, 2], | |
'setC': [5, 5], | |
'setD': [5, 6], | |
} | |
@classmethod | |
def setUpClass(cls): | |
print("***{} started***".format(cls.__name__)) | |
def test_a(self): | |
for name, (a, b) in self.testsmap.items(): | |
with self.subTest(name=name): | |
print('Test run: ', name) | |
print(a) | |
a += 1 | |
print(a) | |
assert a == b, name | |
@classmethod | |
def tearDownClass(cls): | |
print("***{} ended***".format(cls.__name__)) | |
""" | |
Example of test that changes the state of variable and | |
makes it accessible to next tests | |
""" | |
class TestChangeClassAttribute(unittest.TestCase): | |
foo = 'bar' | |
@classmethod | |
def setUpClass(cls): | |
print("***{} started***".format(cls.__name__)) | |
def setUp(self): | |
print("Setup of test") | |
pass | |
def test_a(self): | |
print("Setting foo to can") | |
self.assertEqual(self.__class__.foo, 'bar') | |
self.__class__.foo = 'can' | |
def test_f(self): | |
print("Checking if its still all right") | |
self.assertEqual(self.__class__.foo, 'can') | |
def test_z(self): | |
print(self.__class__.foo) | |
@classmethod | |
def tearDownClass(cls): | |
print("***{} ended***".format(cls.__name__)) | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment