Skip to content

Instantly share code, notes, and snippets.

@arsenlosenko
Created August 11, 2017 13:45
Show Gist options
  • Save arsenlosenko/f12e59830aa4febf77c8a1c009ab8840 to your computer and use it in GitHub Desktop.
Save arsenlosenko/f12e59830aa4febf77c8a1c009ab8840 to your computer and use it in GitHub Desktop.
Unusual fixes or usecase for python unittest library
#!/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