Basic python unittesting


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  1.  
  2. import random
  3. import unittest
  4.  
  5. class TestSequenceFunctions(unittest.TestCase):
  6.  
  7. def setUp(self):
  8. self.seq = range(10)
  9.  
  10. def testshuffle(self):
  11. # make sure the shuffled sequence does not lose any elements
  12. random.shuffle(self.seq)
  13. self.seq.sort()
  14. self.assertEqual(self.seq, range(10))
  15.  
  16. def testchoice(self):
  17. element = random.choice(self.seq)
  18. self.assert_(element in self.seq)
  19.  
  20. def testsample(self):
  21. self.assertRaises(ValueError, random.sample, self.seq, 20)
  22. for element in random.sample(self.seq, 5):
  23. self.assert_(element in self.seq)
  24.  
  25. if __name__ == '__main__':
  26. unittest.main()
  27.  

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.