Created
July 15, 2020 18:44
-
-
Save yrevar/ab52fce3a5b2c392188e0384668b4833 to your computer and use it in GitHub Desktop.
python unittest example
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
# python3 -m unittest_example | |
# python3 -m unittest -v unittest_example | |
# python3 -m unittest -v unittest_example.Numbers.test_odd | |
import unittest | |
class Numbers(unittest.TestCase): | |
def test_20(self): | |
x = 13 | |
self.assertEqual(x, 20) | |
def test_2(self): | |
x = 13 | |
self.assertEqual(x, 2) | |
def test_a(self): | |
x = 'a' | |
self.assertEqual(x, 'a') | |
def test_odd(self): | |
x = 13 | |
self.assertTrue(x % 2 == 1) | |
def test_even(self): | |
x = 13 | |
self.assertTrue(x % 2 == 0) | |
def test_1(self): | |
x = 13 | |
self.assertEqual(x, 1) | |
def test_99(self): | |
x = 13 | |
self.assertEqual(x, 99) | |
if __name__ == '__main__': | |
unittest.main() | |
""" | |
# Example 1: | |
python3 -m unittest -v unittest_example.Numbers.test_odd | |
test_odd (unittest_example.Numbers) ... ok | |
---------------------------------------------------------------------- | |
Ran 1 test in 0.000s | |
OK | |
# Example 2 (notice ordering of tests): | |
python3 -m unittest -v unittest_example | |
test_1 (unittest_example.Numbers) ... FAIL | |
test_2 (unittest_example.Numbers) ... FAIL | |
test_20 (unittest_example.Numbers) ... FAIL | |
test_99 (unittest_example.Numbers) ... FAIL | |
test_a (unittest_example.Numbers) ... ok | |
test_even (unittest_example.Numbers) ... FAIL | |
test_odd (unittest_example.Numbers) ... ok | |
====================================================================== | |
FAIL: test_1 (unittest_example.Numbers) | |
---------------------------------------------------------------------- | |
Traceback (most recent call last): | |
File ".../unittest_example.py", line 54, in test_1 | |
self.assertEqual(x, 1) | |
AssertionError: 13 != 1 | |
====================================================================== | |
FAIL: test_2 (unittest_example.Numbers) | |
---------------------------------------------------------------------- | |
Traceback (most recent call last): | |
File ".../unittest_example.py", line 38, in test_2 | |
self.assertEqual(x, 2) | |
AssertionError: 13 != 2 | |
====================================================================== | |
FAIL: test_20 (unittest_example.Numbers) | |
---------------------------------------------------------------------- | |
Traceback (most recent call last): | |
File ".../unittest_example.py", line 34, in test_20 | |
self.assertEqual(x, 20) | |
AssertionError: 13 != 20 | |
====================================================================== | |
FAIL: test_99 (unittest_example.Numbers) | |
---------------------------------------------------------------------- | |
Traceback (most recent call last): | |
File ".../unittest_example.py", line 58, in test_99 | |
self.assertEqual(x, 99) | |
AssertionError: 13 != 99 | |
====================================================================== | |
FAIL: test_even (unittest_example.Numbers) | |
---------------------------------------------------------------------- | |
Traceback (most recent call last): | |
File ".../unittest_example.py", line 50, in test_even | |
self.assertTrue(x % 2 == 0) | |
AssertionError: False is not true | |
---------------------------------------------------------------------- | |
Ran 7 tests in 0.001s | |
FAILED (failures=5) | |
# Example 3: | |
python3 -m unittest unittest_example | |
FFFF.F. | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment