笔记-unittest group test
笔记-unittest group test
1. 前言
针对单一文件的测试类及测试用例是比较简单的结构,书写非常简单。
但对于较多文件的测试时,需要一定的组织结构,unittest也为此提供了支持。
2. test_suite
实例化类,添加测试方法,
实例化TextTestRunner,run()
手动添加用例,可以保证测试执行顺序。
import unittest
from test.test_webapi import
TestCaseWebApi
def run_suite():
suite = unittest.TestSuite()
# 添加单个测试用例
suite.addTest(TestCaseWebApi('test_webapi_mainpage'))
# 添加多个测试用例
#
suite.addTests([TestCase_WebApi('test_webapi_get')])
# 声明
runner = unittest.TextTestRunner()
runner.run(suite)
if __name__ == '__main__':
run_suite()
2.1. 类及属性
class unittest.TestSuite(tests=())
简而言之,它是一个测试用例组合的类。
addTest(test)
Add a TestCase or TestSuite to the suite.
addTests(tests)
Add all the tests from an iterable of TestCase and TestSuite instances to this test suite.
This is equivalent to iterating over tests, calling addTest() for each element.
类似于对每个用例使用了addTest方法。
TestSuite shares the following methods with TestCase:
run(result)
Run the tests associated with this suite, collecting the result into the test result object passed as result. Note that unlike TestCase.run(), TestSuite.run() requires the result object to be passed in.
debug()
Run the tests associated with this suite without collecting the result. This allows exceptions raised by the test to be propagated to the caller and can be used to support running tests under a debugger.
countTestCases()
Return the number of tests represented by this test object, including all individual tests and sub-suites.
注意:run()方法需要提供参数用于存储运行结果,一般不用这么麻烦,有更好的集成方式,也就是下面的TestLoader。
这里是suite.run()实测:
res = unittest.TestResult()
suite.run(res)
print(res)
3. TestLoader
测试方法,用例,套件,解决了测试方法的层级问题
但上面的手动添加用例还不够方便,能不能自动添加测试方法
class unittest.TestLoader
The TestLoader class is used to create test suites from classes and modules. Normally, there is no need to create an instance of this class; the unittest module provides an instance that can be shared asunittest.defaultTestLoader. Using a subclass or instance, however, allows customization of some configurable properties.
testloader提供了多种查找及添加用例的方法,但一般用不到这么深入
有一个非常简单的方式:
def run_testloader():
print('use TestLoader')
suite =
unittest.defaultTestLoader.discover('.', pattern='test_unittest_*.py', top_level_dir=None)
print(suite)
runner = unittest.TextTestRunner()
runner.run(suite)
if __name__ == '__main__':
# run_suite()
run_testloader()
unittest.defaultTestLoader是testloader的一个实例。
3.1. 类及属性
class unittest.TestLoader
The TestLoader class is used to create test suites from classes and modules. Normally, there is no need to create an instance of this class; the unittest module provides an instance that can be shared as unittest.defaultTestLoader. Using a subclass or instance, however, allows customization of some configurable properties.
用于创建测试套件。
TestLoader objects have the following attributes:
errors
A list of the non-fatal errors encountered while loading tests. Not reset by the loader at any point. Fatal errors are signalled by the relevant a method raising an exception to the caller. Non-fatal errors are also indicated by a synthetic test that will raise the original error when run.
TestLoader objects have the following methods:
loadTestsFromTestCase(testCaseClass)
Return a suite of all test cases contained in the TestCase-derived testCaseClass.
从testcase类中加载测试用例。
A test case instance is created for each method named by getTestCaseNames(). By default these are the method names beginning with test. If getTestCaseNames() returns no methods, but the runTest() method is implemented, a single test case is created for that method instead.
loadTestsFromModule(module, pattern=None)
Return a suite of all test cases contained in the given module. This method searches module for classes derived from TestCase and creates an instance of the class for each test method defined for the class.
从模板中加载测试用例。
loadTestsFromName(name, module=None)
Return a suite of all test cases given a string specifier.
loadTestsFromNames(names, module=None)
Similar to loadTestsFromName(), but takes a sequence of names rather than a single name. The return value is a test suite which supports all the tests defined for each name.
getTestCaseNames(testCaseClass)
Return a sorted sequence of method names found within testCaseClass; this should be a subclass of TestCase.
example:
print(loader.getTestCaseNames(PipelinePymongodbClassTestCase))
discover(start_dir, pattern='test*.py', top_level_dir=None)
Find all the test modules by recursing into subdirectories from the specified start directory, and return a TestSuite object containing them. Only test files that match pattern will be loaded. (Using shell style pattern matching.) Only module names that are importable (i.e. are valid Python identifiers) will be loaded.
The following attributes of a TestLoader can be configured either by subclassing or assignment on an instance:
testMethodPrefix
String giving the prefix of method names which will be interpreted as test methods. The default value is 'test'.
This affects getTestCaseNames() and all the loadTestsFrom*() methods.
简单来说,改变原定义的测试用例方法识别字符串。
sortTestMethodsUsing
Function to be used to compare method names when sorting them in getTestCaseNames() and all the loadTestsFrom*() methods.
suiteClass
Callable object that constructs a test suite from a list of tests. No methods on the resulting object are needed. The default value is the TestSuite class.
This affects all the loadTestsFrom*() methods.
testNamePatterns
List of Unix shell-style wildcard test name patterns that test methods have to match to be included in test suites (see -v option).
4. 附:其它类
4.1. TestResult
class unittest.TestResult
This class is used to compile information about which tests have succeeded and which have failed.
简单来说,就是测试后返回的结果类。
常用的有查看skiped,testrun等
也可以在开始测试和结束前做一些事,具体信息查看startTest,stopTest等方法
A TestResult object stores the results of a set of tests. The TestCase and TestSuite classes ensure that results are properly recorded; test authors do not need to worry about recording the outcome of tests.
Testing frameworks built on top of unittest may want access to the TestResult object generated by running a set of tests for reporting purposes; a TestResult instance is returned by the TestRunner.run() method for this purpose.
TestResult instances have the following attributes that will be of interest when inspecting the results of running a set of tests:
errors
A list containing 2-tuples of TestCase instances and strings holding formatted tracebacks. Each tuple represents a test which raised an unexpected exception.
failures
A list containing 2-tuples of TestCase instances and strings holding formatted tracebacks. Each tuple represents a test where a failure was explicitly signalled using the TestCase.assert*() methods.
skipped
A list containing 2-tuples of TestCase instances and strings holding the reason for skipping the test.
expectedFailures
A list containing 2-tuples of TestCase instances and strings holding formatted tracebacks. Each tuple represents an expected failure of the test case.
unexpectedSuccesses
A list containing TestCase instances that were marked as expected failures, but succeeded.
shouldStop
Set to True when the execution of tests should stop by stop().
testsRun
The total number of tests run so far.
buffer
If set to true, sys.stdout and sys.stderr will be buffered in between startTest() and stopTest() being called. Collected output will only be echoed onto the real sys.stdout and sys.stderr if the test fails or errors. Any output is also attached to the failure / error message.
failfast
If set to true stop() will be called on the first failure or error, halting the test run.
tb_locals
If set to true then local variables will be shown in tracebacks.
wasSuccessful()
Return True if all tests run so far have passed, otherwise returns False.
Changed in version 3.4: Returns False if there were any unexpectedSuccesses from tests marked with the expectedFailure() decorator.
stop()
This method can be called to signal that the set of tests being run should be aborted by setting the shouldStop attribute to True. TestRunner objects should respect this flag and return without running any additional tests.
For example, this feature is used by the TextTestRunner class to stop the test framework when the user signals an interrupt from the keyboard. Interactive tools which provide TestRunner implementations can use this in a similar manner.
The following methods of the TestResult class are used to maintain the internal data structures, and may be extended in subclasses to support additional reporting requirements. This is particularly useful in building tools which support interactive reporting while tests are being run.
startTest(test)
Called when the test case test is about to be run.
stopTest(test)
Called after the test case test has been executed, regardless of the outcome.
startTestRun()
Called once before any tests are executed.
stopTestRun()
Called once after all tests are executed.
addError(test, err)
Called when the test case test raises an unexpected exception. err is a tuple of the form returned by sys.exc_info(): (type, value, traceback).
The default implementation appends a tuple (test, formatted_err) to the instance’s errors attribute, where formatted_err is a formatted traceback derived from err.
addFailure(test, err)
Called when the test case test signals a failure. err is a tuple of the form returned by sys.exc_info(): (type, value, traceback).
The default implementation appends a tuple (test, formatted_err) to the instance’s failures attribute, where formatted_err is a formatted traceback derived from err.
addSuccess(test)
Called when the test case test succeeds.
The default implementation does nothing.
addSkip(test, reason)
Called when the test case test is skipped. reason is the reason the test gave for skipping.
The default implementation appends a tuple (test, reason) to the instance’s skipped attribute.
addExpectedFailure(test, err)
Called when the test case test fails, but was marked with the expectedFailure() decorator.
The default implementation appends a tuple (test, formatted_err) to the instance’s expectedFailures attribute, where formatted_err is a formatted traceback derived from err.
addUnexpectedSuccess(test)
Called when the test case test was marked with the expectedFailure() decorator, but succeeded.
The default implementation appends the test to the instance’s unexpectedSuccesses attribute.
addSubTest(test, subtest, outcome)
Called when a subtest finishes. test is the test case corresponding to the test method. subtest is a custom TestCase instance describing the subtest.
If outcome is None, the subtest succeeded. Otherwise, it failed with an exception where outcome is a tuple of the form returned by sys.exc_info(): (type, value, traceback).
The default implementation does nothing when the outcome is a success, and records subtest failures as normal failures.
4.2. unittest.TextTestRunner()
简单来说,是一个输出测试结果到文件的类。
class unittest.TextTestRunner(stream=None, descriptions=True, verbosity=1, failfast=False, buffer=False, resultclass=None, warnings=None, *, tb_locals=False)
A basic test runner implementation that outputs results to a stream. If stream is None, the default, sys.stderr is used as the output stream. This class has a few configurable parameters, but is essentially very simple. Graphical applications which run test suites should provide alternate implementations. Such implementations should accept **kwargs as the interface to construct runners changes when features are added to unittest.
By default this runner shows DeprecationWarning, PendingDeprecationWarning, ResourceWarning and ImportWarning even if they are ignored by default. Deprecation warnings caused by deprecated unittest methods are also special-cased and, when the warning filters are 'default' or 'always', they will appear only once per-module, in order to avoid too many warning messages. This behavior can be overridden using Python’s -Wd or -Wa options (see Warning control) and leaving warnings to None.
_makeResult()
This method returns the instance of TestResult used by run(). It is not intended to be called directly, but can be overridden in subclasses to provide a custom TestResult.
_makeResult() instantiates the class or callable passed in the TextTestRunner constructor as the resultclass argument. It defaults to TextTestResult if no resultclass is provided. The result class is instantiated with the following arguments:
stream, descriptions, verbosity
run(test)
This method is the main public interface to the TextTestRunner. This method takes a TestSuite or TestCase instance. A TestResult is created by calling _makeResult() and the test(s) are run and the results printed to stdout.
4.2.1. example
输出测试结果到文件:
def run_suite():
#runner = unittest.TextTestRunner(verbosity=2)
#res = runner.run(suite_make())
#print(type(res),res.skipped)
with open('result.txt', 'w', encoding='utf-8') as fi:
runner = unittest.TextTestRunner(verbosity=2, stream=fi)
runner.run(suite_make())
if __name__ == "__main__":
run_suite()
注释部分的功能与with代码块的功能差不多。
4.3. unittest.main()
这是一个单模板测试的简单方法。
unittest.main(module='__main__', defaultTest=None, argv=None, testRunner=None, testLoader=unittest.defaultTestLoader, exit=True, verbosity=1, failfast=None, catchbreak=None, buffer=None, warnings=None)
A command-line program that loads a set of tests from module and runs them; this is primarily for making test modules conveniently executable. The simplest use for this function is to include the following line at the end of a test script:
if __name__ == '__main__':
unittest.main()
You can run tests with more detailed information by passing in the verbosity argument:
if __name__ == '__main__':
unittest.main(verbosity=2)
The defaultTest argument is either the name of a single test or an iterable of test names to run if no test names are specified via argv. If not specified or None and no test names are provided via argv, all tests found in module are run.
The argv argument can be a list of options passed to the program, with the first element being the program name. If not specified or None, the values of sys.argv are used.
5. code
5.1. 单一文件测试
非常简单,类,用例,运行。
#coding:utf-8
"""
----------------------------------------
description:
author: sss
date:
----------------------------------------
change:
----------------------------------------
"""
__author__ = 'sss'
import unittest
class DBmongodbTestCase(unittest.TestCase):
"""
db 测试
主要是测试mongodb是否可用
"""
@classmethod
def setUpClass(cls):
try:
from db.db_mongodb
import db_conn
#cls.db_conn =
db_conn
setattr(cls, 'db_conn', db_conn)
except Exception:
print('import
db_conn failed.')
def test_fun_conn(self):
"""
function for test db_conn()
测试是否可连接mongodb并查询
"""
from pymongo.database
import Database
db = self.db_conn()
#print(type(db))
self.assertEqual(isinstance(db,
Database), True, 'test error')
coll_list =
db.list_collection_names()
#print(coll_list)
self.assertEqual(isinstance(coll_list,list),True,'test
error')
def start_test():
print('start
test.')
unittest.main()
print('test
end.')
if __name__ == "__main__":
start_test()
5.2. suite使用
关键地方是使用了TextLoader类,毕竟去每个测试类中一一加载测试用例太繁琐了。
#coding:utf-8
"""
----------------------------------------
description:
author: sss
date:
----------------------------------------
change:
----------------------------------------
"""
__author__ = 'sss'
import unittest
from test_unit.test_db import DBmongodbTestCase
from test_unit.test_pipeline import PipelinePymongodbClassTestCase,PipelineMysqlClassTestCase
def suite_make():
loader = unittest.TestLoader()
suite = unittest.TestSuite()
test_cases = (DBmongodbTestCase,
PipelinePymongodbClassTestCase,
PipelineMysqlClassTestCase)
for test_class
in test_cases:
tests =
loader.loadTestsFromTestCase(test_class)
suite.addTest(tests)
return suite
def run_suite():
#runner =
unittest.TextTestRunner(verbosity=2)
#res = runner.run(suite_make())
#print(type(res),res.skipped)
with open('result.txt', 'w', encoding='utf-8') as fi:
runner = unittest.TextTestRunner(verbosity=2, stream=fi)
runner.run(suite_make())
if __name__ == "__main__":
run_suite()
#suite_make()

浙公网安备 33010602011771号