-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathunittest_usage.py
91 lines (79 loc) · 3.05 KB
/
unittest_usage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"""
Python unittest tips:
Method Checks that New in
assertEqual(a, b) a == b
assertNotEqual(a, b) a != b
assertTrue(x) bool(x) is True
assertFalse(x) bool(x) is False
assertIs(a, b) a is b 2.7
assertIsNot(a, b) a is not b 2.7
assertIsNone(x) x is None 2.7
assertIsNotNone(x) x is not None 2.7
assertIn(a, b) a in b 2.7
assertNotIn(a, b) a not in b 2.7
assertIsInstance(a, b) isinstance(a, b) 2.7
assertNotIsInstance(a, b) not isinstance(a, b) 2.7
assertRaises(exc, fun, *args, **kwds)
assertRaisesRegexp(exc, re, fun, *args, **kwds)
assertAlmostEqual(a, b)
assertNotAlmostEqual(a, b)
assertGreater(a, b)
assertGreaterEqual(a, b)
assertLess(a, b)
assertLessEqual(a, b)
assertRegexpMatches(s, re)
assertNotRegexpMatches(s, re)
assertItemsEqual(a, b)
assertDictContainsSubset(a, b)
assertMultiLineEqual(a, b)
assertSequenceEqual(a, b)
assertListEqual(a, b)
assertTupleEqual(a, b)
assertSetEqual(a, b)
assertDictEqual(a, b)
"""
import random
import unittest
import sys
__version__ = "3.4.2"
class TestSequenceFunctions(unittest.TestCase):
"""
Basic example of unittest from http://docs.python.org/2/library/unittest.html
"""
def setUp(self):
self.seq = range(10)
def tearDown(self):
self.seq = None
def test_shuffle(self):
# make sure the shuffled sequence does not lose any elements
random.shuffle(self.seq)
self.seq.sort()
self.assertEqual(self.seq, range(10))
# should raise an exception for an immutable sequence
self.assertRaises(TypeError, random.shuffle, (1, 2, 3))
def test_choice(self):
element = random.choice(self.seq)
self.assertTrue(element in self.seq)
def test_sample(self):
with self.assertRaises(ValueError):
random.sample(self.seq, 20)
for element in random.sample(self.seq, 5):
self.assertTrue(element in self.seq)
# Skipping tests
@unittest.skip("demonstrating skipping")
def test_nothing(self):
self.fail("shouldn't happen")
# Skipping test on some conditions
@unittest.skipIf(__version__ < (1, 3), "feature not supported in this library version")
def test_format(self):
# Tests that work for only a certain version of the library.
pass
# Skipping test on non-windows systems
@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
def test_windows_support(self):
# windows specific testing code
pass
if __name__ == '__main__':
unittest.main()
#suite = unittest.TestLoader().loadTestsFromTestCase(TestSequenceFunctions)
#unittest.TextTestRunner(verbosity=2).run(suite)