Python unit test loading and skipping test methods and examples

Preface

In the python unittest framework, standard functions for use case loading and skipping are built in.

Its load case is implemented by the TestLoader class, while the skip test method is implemented by the unittest.skip() class. Now let's study together.

TestLoader Load Use Case

The TestLoader class has a discover() function, which concisely implements loading test cases from specified top-level directories, modules, etc.

Save the following code to == test==_disvover.py

# _*_ coding:utf-8 _*_

__author__ = 'Bitter leaves'

import unittest
import sys
reload(sys)
sys.setdefaultencoding("utf-8")


# Measured function
def add(a, b):
    return a + b

# test case
class demoTest(unittest.TestCase):

    def test_add_4_5(self):
        print u"test discover command"
        self.assertEquals(add(4,5),9)

Switch to the directory where==test==_disvover.py is located and execute the command from the command line:

python -m unittest discover

The results are as follows:


discover.png

With this command, the unittest test test framework will try to load all required tests in the current directory and its subdirectories (=== Note: All use case modules should be named at the beginning of test==, and modules starting with test should be loaded by default).

Now let's look at the other related commands.

Serial number Command Options describe
1 -v, --verbose Detailed output
2 -s, --start-directory Start directory (default is current directory)
3 -p, --pattern Match loaded test files (default match test*.py)
4 -t, --top-level-directory Top-level directory (default same -- start-directory)

For example, we specify C: test to match all test modules that start with assert

python -m unittest -v -s "c:\\test" -p "assert*.py"

This command loads the test methods in all test modules that begin with assert in the C: test directory

unittest.skip test method

The Python unit test framework has supported setting up test cases that skip specified test methods or satisfy certain conditions since Python 2.7.

Let me see an example of forcing a test case to skip a specified test case:

# _*_ coding:utf-8 _*_

__author__ = 'Bitter leaves'

import unittest
import sys
reload(sys)
sys.setdefaultencoding("utf-8")

# Measured function
def add(a, b):
    return a + b

class demoSkipTest(unittest.TestCase):

    @unittest.skip(u"Forced skip example")
    def test_add(self):
        self.assertEqual(add(4,5), 9)

    def test_add_2(self):
        self.skipTest("Forced skip example 2")
        self.assertEqual(add(4,5), 9)

if __name__ == '__main__':
    unittest.main()

Save the above code to demoskip.py and run the following command:

python demoskip.py

The results are as follows:


demoskip.png

Let's take a look at the various skip methods.

Serial number Method Explain
1 unittest.skip(reason) Forced jump. reason is the cause of jump
2 unittest.skipIf(condition, reason) Conditional jump, jump if condition is True
3 unittest.skipUnless(condition, reason) Unless conditioin is True, adjustments are made
4 unittest.expectedFailure() Marking the test as expected to fail does not count as a failure if the test method fails

Let's look at examples of various practices

# _*_ coding:utf-8 _*_

__author__ = 'Bitter leaves'

import unittest
import sys
reload(sys)
sys.setdefaultencoding("utf-8")

class demoSkipTest(unittest.TestCase):
    a = 50
    b = 20

    def test_add(self):
        """addition"""
        result = self.a + self.b
        self.assertEqual(result, 40)

    @unittest.skipIf(a>b, u"a>b Just skip")    
    def test_sub(self):
        """subtraction"""
        result = self.a - self.b
        self.assertTrue(result == -30)

    @unittest.skipUnless(b==0, u"Divisor 0, jump")
    def test_div(self):
        """division"""
        result = self.a / self.b
        self.assertTrue(result == 1)

    @unittest.expectedFailure
    def test_mul(self):
        """multiplication"""
        result = self.a * self.b
        self.assertTrue(result == 0)

if __name__ == "__main__":
    unittest.main()

Save the above code to demo_skip_test.py, and the results are as follows


allskip.png

summary

This time, we share the use case loading and how to skip some test methods or use cases. We can learn and practice the modification based on the above example code.

Keywords: Python

Added by kardiostep on Tue, 09 Jul 2019 21:51:17 +0300