Find something useful online about unittest:
unittest.skip(reason)
- unittest.skipIf(condition, reason)
- Skip the decorated test if condition is true.
- unittest.skipUnless(condition, reason)
- Skip the decorated test unless condition is true.
- unittest.expectedFailure()
- Mark the test as an expected failure. If the test fails when run, the test is not counted as a failure.
- unittest.SkipTest(reason)
- This exception is raised to skip a test.
Maybe it is not clear how to use it. I will show you a homemade example.
import random
import unittest
def #this the function you want to test define youself
#in oreder for convenience, we test the whether f(x,y)= x/y is right
class Test(unittest.TestCase):
@unittest.skipIf(b==0, "0 can not divide")
def test_shuffle(self):
self.assertEqual(f(1,5), 0.2)
expectedFailure(f(1,2),3)
a=random.choice(range(10))
b=random.choice(self.seq(10))
self.assertEqual(f(b,a), b/a
These are really interesting methods about unittest. It seems we can test our functions more efficiently. Good to know the new stuff:)
ReplyDelete