Improve the basic example.

* Show both the decorator and regular form for assertRaises()
* Use assertTrue() instead of assertIn() to teach useful minimal subset of the API
This commit is contained in:
Raymond Hettinger 2010-03-09 08:44:18 +00:00
parent f4803aa623
commit 08090bf36a
1 changed files with 7 additions and 3 deletions

View File

@ -116,14 +116,18 @@ Here is a short script to test three functions from the :mod:`random` module::
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.assertIn(element, self.seq)
self.assertTrue(element in self.seq)
def test_sample(self):
self.assertRaises(ValueError, random.sample, self.seq, 20)
with self.assertRaises(ValueError):
random.sample(self.seq, 20)
for element in random.sample(self.seq, 5):
self.assertIn(element, self.seq)
self.assertTrue(element in self.seq)
if __name__ == '__main__':
unittest.main()