Python Series acquaintance unittest

unittest unit test frame may be applicable not only to the test unit, can be applied WEB automated test development and execution of the test frame may be organized to execute test cases, and provides a rich assertions, by determining whether the test case, the test results to generate the final . Section on the basics of python and unittest not elaborate, we are free to Baidu in-depth understanding Oh, here is the entire unittest four more important concepts.

test fixture

test case

test suite

test runner

test fixture: in short, to do some things during the test need to be prepared, such as creating temporary databases, files and directories, which setUp () and setDown () is the most commonly used method.
test case: the base class test methods user-defined case, call run () method in turn calls the setUP method, execution of the embodiment, the tearDown () method.
test suite: a set of test cases to be () method for increasing the Test Case manually by addTest, may be automatically added by the Test Case TestLoader, TestLoader use when adding embodiment will be no order.
test runner: running test driver class may be performed TestCase, also perform TestSuite. After performing TestCase and Testsuite automatically manages TestResult.

Paste The following sample code is simple unittest:

import unittest
import HTMLTestRunner
import time
class TestCases(unittest.TestCase):
    def setUp(self):
        print("每一条case执行之前都会执行这个方法")
    def tearDown(self):
        print("每一条case执行之后都会执行这个方法\n")
    def test_testcase1(self):
        print("这是一条测试用例case1")
        a = "hello"
        try:
            self.assertTrue(a.isalpha())
            print("测试通过")
        except Exception as e:
            print("出错啦,错误结果是%s" % e)
            print("failed")
            raise e
    def test_testcase2(self):
        print("这是一条测试用例case2")
        b = "123"
        try:
            self.assertTrue(b.isalpha())
            print("测试通过")
        except Exception as e:
            print("出错啦,错误结果是%s" % e)
            print("failed")
            raise e
    def test_testcase3(self):
        print("这是一条测试用例case3")
        c = " "
        try:
            self.assertTrue(c.isalpha())
            print("测试通过")
        except Exception as e:
            print("出错啦,错误结果是%s" % e)
            print("failed")
            raise e
'''
if __name__ == "__main__":
    unittest.main()
    写上这段代码可以在命令行直接运行model.py,而且用例全部执行
'''
"""
加载测试用例,方法不仅这种,还有利用loader的方法
loader = unittest.TestLoader()
suite.addTest(loader.loadTestsFromTestCase(TestCases))
or 
suite.addTest(loader.loadTestsFromModule(model))
"""
suite = unittest.TestSuite()
# suite.addTest("test_testcase2")   # 这里我写错了,要加上模块
suite.addTest(TestCases('test_testcase3'))
"""
指定测试报告的路径并且定义报告名称格式
"""
report_dir = "../Test report"
now = time.strftime("%Y-%m-%d %H-%M-%S")
reportname = report_dir + "/" + now + " Test report.html"
"""
运行用例并生成测试报告
"""
with open(reportname, "wb+") as file:
    runner = HTMLTestRunner.HTMLTestRunner(file, 2, title="Model test report",
                                           description="Hello testers! This is the description of Model test"
                                                       "report")
    runner.run(suite)

unittest module Summary:

About this module, this module learn before his time, it is not very good, these two days is finally a slight gain. Example, when I was doing a new discovery: You can not try to assert into the statement and can be included in the class except exception, otherwise it can not be set by asserting the result of the test case.
Python Series acquaintance unittest

Rui Jiang Yunguan website link: http://www.eflycloud.com/#register?salesID=6DGNUTUAV

Guess you like

Origin blog.51cto.com/13475644/2429070