Saturday, February 22, 2014

week5

There is really nothing totalk about this week....

Find something useful online about unittest:

unittest.skip(reason)
Unconditionally skip the decorated test. reason should describe why the test is being skipped.

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

1 comment:

  1. These are really interesting methods about unittest. It seems we can test our functions more efficiently. Good to know the new stuff:)

    ReplyDelete