From: Raymond Hettinger Date: Tue, 9 Mar 2010 08:44:18 +0000 (+0000) Subject: Improve the basic example. X-Git-Tag: v2.7b1~408 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=08090bf36ad25b6fd00fb0fa46368d98e7b3b317;p=thirdparty%2FPython%2Fcpython.git 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 --- diff --git a/Doc/library/unittest.rst b/Doc/library/unittest.rst index 68ad8ccbf2d6..5a17d465c3e1 100644 --- a/Doc/library/unittest.rst +++ b/Doc/library/unittest.rst @@ -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()