This is the 25th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

1. Command line operations

1. Run the test module

python -m unittest test_module1 test_module2

Example: python -m unittest test_Demo

Run result: execute test module test_Demo

2. Run the test class

python -m unittest test_module.TestClass

For example, python -m unittest test_demo. TestDemo

Result: Execute TestDemo test class in test_Demo test module

3. Execute the test method

python -m unittest test_module.TestClass.test_method

For example, python -m unittest test_demo.testdemo. test_case2

Run result: execute test module test_Demo test class test_case2 test method.

4. Print details

When executing a test module/class/method, add the -v argument to print the details.

python -m unittest -v test_module

For example, python -m unittest -v test_Demo

Run result: execute test module test_Demo, print detailed information

5. Automatic search execution

Unittest supports simple test searches. When the command enters the project, the framework automatically searches the current directory for the use case to test and executes it. The search directory must be a package or module, and the default match rule for test case files is test*.py

python -m unittest discover

The current project directory has only one module (test_demo.py).

Second, the assertion

In the process of executing a test case, whether the final test case succeeds or not is determined by judging whether the actual result obtained from the test is equal to the expected result. In this case, the assertion method is used.

Common assertion methods:

assertEqual(a,b,[msg='Messages printed when tests fail']) asserts whether a and B are equal, and if so, the test case passes. assertNotEqual(a,b,[msg='Messages printed when tests fail']) asserts whether a and B are equal, if not, the test case passes. assertTrue(x,[msg='Messages printed when tests fail']) assert whether xTrue, it isTrueThe test case passes. assertFalse(x,[msg='Messages printed when tests fail']) assert whether xFalse, it isFalseThe test case passes. assertIs(a,b,[msg='Messages printed when tests fail']) assert whether a is B, if yes, the test case passes. assertNotIs(a,b,[msg='Messages printed when tests fail']) asserts whether a is B, if not, the test case passes. assertIsNone(x,[msg='Messages printed when tests fail']) assert whether xNone, it isNoneThe test case passes. assertIsNotNone(x,[msg='Messages printed when tests fail']) assert whether xNone, notNoneThe test case passes. assertIn(a,b,[msg='Messages printed when tests fail']) asserts whether A is in B, where the test case passes. assertNotIn(a,b,[msg='Messages printed when tests fail']) asserts whether a is in B, if not, the test case passes. assertIsInstance(a,b,[msg='Messages printed when tests fail']) assert whether a is an instance of B. If yes, the test case passes. assertNotIsInstance(a,b,[msg='Messages printed when tests fail']) asserts whether a is an instance of B, if not, the test case passes.Copy the code

Three, automatic adjustment of test cases

Unittest provides decorator methods that skip use cases

1.@unittest.skip(reason)Skip the use cases unconditionally

import unittest 

class TestCan(unittest.TestCase) : 
    # skip skip unconditionally
    @unittest.skip("Skip test case test_01") 
    def test_01(self) : 
        print("hello world1") 
        
    def test_02(self) : 
        print("hello world2") 
        
if __name__ == '__main__': 
    unittest.main(verbosity=2)
Copy the code

2.@unittest.skipIf(condition, reason)If the condition is true, the test is skipped

import unittest 

class TestCan(unittest.TestCase) :

    # skip skip unconditionally
    @unittest.skip("Skip test case test_01") 
    def test_01(self) : 
        print("hello world1") 
        
    def test_02(self) : 
        print("hello world2") 
        
    @unittest.skipIf(True."Condition not enforceable.") 
    def test_03(self) : 
        print("hello world3")
Copy the code

3.@unittest.skipUnless(condition, reason) If the conditions are not met, the test is skipped

import unittest 

class TestCan(unittest.TestCase) :

    # skip skip unconditionally
    @unittest.skip("Skip test case test_01") 
    def test_01(self) : 
        print("hello world1") 
        
    def test_02(self) : 
        print("hello world2") 
        
    @unittest.skipIf(True."Condition satisfied skip use case") 
    def test_03(self) : 
        print("hello world3") 
        
    @unittest.skipUnless(False."Condition not met skip use case") 
    def test_04(self) : 
        print("test04") 
        
if __name__ == '__main__': 
    unittest.main(verbosity=2)
Copy the code

4. Skip test classes

import unittest

@unittest.skip(Jump jump jump) 
class TestCan(unittest.TestCase) :

    # skip #
    @unittest.skip("Skip test case test_01") 
    def test_01(self) : 
        print("hello world1") 
        
    def test_02(self) : 
        print("hello world2") 
        
    # @unittest.skipIf(True)
    def test_03(self) : 
        print("hello world3") # 
        
    @unittest.skipUnless(False."Condition not met skip use case") 
    def test_04(self) : 
        print("test04") 
        
 if __name__ == '__main__': 
     unittest.main(verbosity=2)
Copy the code