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