When you run a test, you need to skip some test cases directly, or skip the test if the use case meets a condition, or set the test case to fail directly. The UnitTest Unit Test Framework provides adorners to implement these requirements.
1.unittest.skip (reason)
Skip the decorating test unconditionally, explaining why you skipped the test
2.UNITTEST.SKIPIF (Condition,reason)
If the condition is true, skip decorating the test.
3.unittest.skipuless (Condition,reason)
Skip the decoration test unless the condition is true
4.unittest.expectedfailure ()
The test was marked as failed. Unification is marked as failed regardless of whether the execution result has failed.
The code is as follows:
1 ## Coding =utf-82 ## Calculator3 #4 #class Count ():5 #def __init__ (self, A, b):6 #self.a = Int (a)7 #self.b = Int (b)8 #9 #def Add (self):Ten #return SELF.A + self.b One # A #def sub (self): - #return self.a-self.b - ImportUnitTest the - - classMyTest (unittest. TestCase): - defsetUp (self): + Pass - + defTearDown (self): A Pass at -@unittest. Skip ("skip a test directly") - defTest_skip (self): - Print("Test AAA") - -@unittest. SkipIf (3 > 2,"When the condition is true, the test is skipped") in deftest_skip_if (self): - Print('Test BBB') to +@unittest. skipunless (3 > 2,"When the condition is true, the test is executed") - deftest_skip_unless (self): the Print('Test CCC') * $ @unittest. ExpectedfailurePanax Notoginseng deftest_expected_failure (self): -Assertequal (2, 3) the + A if __name__=='__main__': theUnittest.main ()
As above: The first test case is decorated by @unittest.skip (), skipping directly and not executing.
The second use case is decorated with @unittest.skipif (), and when the condition is true, the 3>2 condition is true (true), skipping does not execute.
The third use case is decorated with @unittest.skipunless () Z, and when the condition is true, the 3>2 condition is true (true), and the third use case executes.
The fourth use case is decorated with @unittest.expectedfailure, which, regardless of whether the execution result fails, is uniformly marked as failed, but does not throw an error message.
These methods can also function as test classes, simply by defining them on the test class.
1 ImportUnitTest2 3 4@unittest. Skip ("Skip the test class directly")5 classMyTest (unittest. TestCase):6 defsetUp (self):7 Pass8 9 defTearDown (self):Ten Pass One A@unittest. Skip ("skip a test directly") - defTest_skip (self): - Print("Test AAA") the -@unittest. SkipIf (3 > 2,"When the condition is true, the test is skipped") - deftest_skip_if (self): - Print('Test BBB') + -@unittest. skipunless (3 > 2,"When the condition is true, the test is executed") + deftest_skip_unless (self): A Print('Test CCC') at - @unittest. Expectedfailure - deftest_expected_failure (self): -Assertequal (2, 3) - - in if __name__=='__main__': -Unittest.main ()
Python+selenium Skip test and expected failure