Unittest command line execution test, a test example of the operation found

Coding UTF8 = #
'' '
can be run from the command line: modules, classes, or the individual test methods, the specific operation is as follows:
Run the test module: python -m unittest test_module1 test_module2
run the test class: python -m unittest test_module.TestClass
run test method: python -m unittest test_module.TestClass.test_method
can add the module name to be run in a list, class names, method names.
You can get more detailed test information by using the -v parameter: python -m unittest -v test_module
can be viewed by all parameters -h command line parameters: python -m unittest -h
command-line arguments:
-b --buffer
during test run the standard output and standard error stream to be cached. Output through the test information.
Output normal echo when the test fails or errors, and added to the failure message.
--catch -c
Ctrl + C to wait for the end of the current test during the test run and report all results to date.
The second Ctrl + C will lead to KeyboardInterrupt exception.
-f --failfast
stop the test run when the first error or malfunction.
Command line discovery can also be used to test (Test Discovery), the test run for all items or only for a subset.
Unittest find support simple test (Test Discovery). In order to be compatible with the test found,
All test files must be imported from the root directory of the project module or package (which means their file name must be a valid identifier).
Test Discovery is () is achieved by TestLoader.discover, it can be realized through command line.
The basic command line usage:
cd project_directory # into the project root directory
python -m unittest discover # execute command
parameters discover sub-command is as follows:
-v, --verbose
verbose output
-s, --start-directory directory
from performing discovery Home directory (directory), the default is the current directory (.)
-p, --pattern pattern
pattern matching test file (pattern), default is * .py the test
-t, --top-Level-directory directory
project (directory) the purpose of the root directory (the default directory is starting)
-s, -p, -t command can be combined in a command line. The following two commands are equivalent:
Python the unittest Discover -s -m project_directory -p "* _test.py"
Python Discover project_directory the unittest -m "* _test.py"
as a path, a package name can be passed, e.g. myproject. subpackage.test as the starting directory.
And then import the package name provided and its location on the file system as the start directory.
Note: Discover the test to load test by importing. Once the test has been found to find all test files from the specified start directory,
it will convert the path name for the package to be imported. E.g. foo / bar / baz.py imported as foo.bar.baz.
If there is a global package installed, tested and try to find a different copy of the software package,
then import that may occur in the wrong place. If this happens, the test found that the warning and exit.
If you provide the starting directory for the package name instead of the directory path is found assume any position from which it is imported the desired position, it will not get a warning.
And a package test module may load custom test and found load_tests protocol.
'' '
Import OS
# TestHello.py into the path of
the os.chdir ( ".// TestSuit_01")
Print The os.getcwd ()
Print' '' via command line test information '' '
# running test module TestHello.py
os.system ( "-m Python unittest TestHello")
# run the test class TestHello
os.system ( "-m Python unittest TestHello.TestHello")
# running the test method
os.system ( "python -m unittest TestHello.TestHello.

Print

Print '' 'can be obtained by using a more detailed test -v parameter information' ''
# running test module TestHello.py
the os.system ( "-v TestHello the unittest Python -m")
# run the test class TestHello
the os.system ( "-v TestHello.TestHello the unittest Python -m")
# run test method for
the os.system ( "-v TestHello.TestHello.test_Hello the unittest Python -m")
Print "#" * 50
Print

Print '' 'by the parameter -h View all command line parameters: -m Python unittest -h '' '
os.system ( "Python unittest -m -h")
Print "#" * 50
Print

Print "command-line arguments: -b -c -f"
# run The test module TestHello.py
os.system ( "Python unittest -m -b TestHello")
# run the test class TestHello
os.system ( "Python unittest -m -c TestHello.TestHello ")
# running the test method
os.system ( "Python unittest -m -f TestHello.TestHello.test_HelloType")
print "#" * 50
Print

# using the find command to perform all tests test
print "test found using the implementation of projects in all tests"
# into UnttestPro
os .chdir ( "../")
Print "at the Current path:", os.getcwd ()
# use all default parameters
os.system ( "python -m unittest discover" )

Original: https: //blog.csdn.net/henni_719/article/details/56835833

Guess you like

Origin www.cnblogs.com/dreamhighqiu/p/11004335.html