Common methods of loading test cases by unittest framework in Python

Posted by urb on Mon, 06 Jan 2020 23:12:46 +0100

unittest provides us with many methods to load use cases. Here are two common methods... Second recommended

The first way to load test cases: load two modules with loader

All modules need to be loaded into the suite

Then it can automatically run all modules

The execution order is based on the order of importing test cases. First, execute num multi, and then execute num add

import unittest

# Import the written test case, and you can use the as Renaming is shorter and easier
from Python_0715_unittest import lemon_06_unittest_two_num_multi as num_multi
from Python_0715_unittest import lemon_07_unittest_two_num_add as num_add

# 1. Create test suite,Use unittest Among them TestSuite (Fast)
one_suite = unittest.TestSuite()

# 2.Bulk loading test cases through modules
# Define test loader object, using unittest Among them TestLoader (Taylor louzel)
one_loader = unittest.TestLoader()

# Using the loader loadTestsFromModule(Building:.Tester.Load test cases from modules
# Then use addTest Put use cases in one_suite Suite
one_suite.addTest(one_loader.loadTestsFromModule(num_multi))
one_suite.addTest(one_loader.loadTestsFromModule(num_add))

# 3.Execution case
# Need to create actuator object, using unittest Among them TextTestRunner(Translation: taist.Teste. Soft there)
one_runner = unittest.TextTestRunner()
# Run kit with actuator
one_runner.run(one_suite)

# In the result of execution, Capitalized F Code failed use cases
# .Represents a successful use case, F Representative failure

The second way to load test cases: discover method

import unittest


# Load test cases first
# Use unittest in defaultTestLoader(Drop: drop fao Of.Tester.Lou Ze discover(Descava) method
# First, use . Represents the current py The path of the file, which is automatically loaded to test initial py File module
# one_suite = unittest.defaultTestLoader.discover(".")    # . represents the path of the current py file

# Second, use absolute path loading, use r Transcoding
one_suite = unittest.defaultTestLoader.discover(r"D:\zj_danyuan\Python_0715_unittest")

# Execution case
# 1.Need to create actuator object, using unittest Among them TextTestRunner(Translation: taist.Tester.Soft there)
one_runner = unittest.TextTestRunner()
# 2.Operation Suite
one_runner.run(one_suite)

# In the result of execution, Capitalized F Code failed use cases
# .Represents a successful use case

 

 

*******Please respect the original, if you want to reprint, please indicate the source: reprint from: https://www.cnblogs.com/shouhu/ Thank you!! * * * * * *

Topics: Python