Let's talk about unit testing

The unit test framework that comes with Python is the unittest module, which includes some methods to verify the returned results and some initialization operations before the execution of the use case;

TestCase is the test case

TestSuite multiple test cases together

TestLoader is used to load TestCase into TestSuite

TestRunner is used to execute test cases, and the test results will be saved in the TestResult instance, including how many test cases were run, how many were successful, and how many failed.

Simple unit test case:

import unittest
def calc(x,y):
    return x+y
class TestCalc(unittest.TestCase):
    def test_pass_case(self):
        '''这个是通过测试用例'''
        print('这个是通过测试用例!')
        res=calc(1,2)
        self.assertEqual(3,res)
        self.assertNotEqual(4,res)#返回的结果不一样
    def test_a(self):
        '''这个是普通的测试用例'''
        print('a')
    def test_fail_case(self):
        '''这个是失败的测试用例'''
        print('这个是失败的测试用例')
        res=calc(3,4)
        self.assertEqual(8,res)
if __name__=='__main__':
    unittest.main()#运行所有的测试用例
import unittest
def calc(x,y):
    return x+y
class TestCalc(unittest.TestCase):
    def test_pass_case(self):
        '''这个是通过测试用例'''
        print('这个是通过测试用例!')
        res=calc(1,2)
        self.assertEqual(3,res)
        self.assertNotEqual(4,res)#返回的结果不一样
    def setUp(self):
        #每个用例运行之前都会执行它
        print('我是setup')
    def tearDown(self):
        #每个用例运行完之后都会执行它
        print('我是tearDown')
    @classmethod
    def setUpClass(cls):
        #所有的用例执行之前执行一次
        print('我是setupClass')
    @classmethod
    def tearDownClass(cls):
        #所有用例都运行完之后执行一次
        print('我是tearDownClass')
    def test_a(self):
        '''这个是普通的测试用例'''
        print('a')
    def test_fail_case(self):
        '''这个是失败的测试用例'''
        print('这个是失败的测试用例')
        res=calc(3,4)
        self.assertEqual(8,res)
    def test_haha(self):
        '''哈哈哈'''
        self.assertEqual(1,2)
if __name__=='__main__':
    unittest.main()

To generate a test report, you need to add another module: HTMLTestRunner. This module needs to be installed by yourself. After executing the test case, an html test report will be generated, and there will be the execution results of each test case:

Copy the HTMLTestRunner.py file to External Libraries—>site-packages in pycharm

import unittest,HTMLTestRunner
def calc(x,y):
    return x+y
class TestCalc(unittest.TestCase):
    def test_pass_case(self):
        '''这个是通过测试用例'''
        print('这个是通过测试用例!')
        res=calc(1,2)
        self.assertEqual(3,res)
        self.assertNotEqual(4,res)#返回的结果不一样
    def setUp(self):
        #每个用例运行之前都会执行它
        print('我是setup')
    def tearDown(self):
        #每个用例运行完之后都会执行它
        print('我是tearDown')
    @classmethod
    def setUpClass(cls):
        #所有的用例执行之前执行一次
        print('我是setupClass')
    @classmethod
    def tearDownClass(cls):
        #所有用例都运行完之后执行一次
        print('我是tearDownClass')
    def test_a(self):
        '''这个是普通的测试用例'''
        print('a')
    def test_fail_case(self):
        '''这个是失败的测试用例'''
        print('这个是失败的测试用例')
        res=calc(3,4)
        self.assertEqual(8,res)
    def test_haha(self):
        '''哈哈哈'''
        self.assertEqual(1,2)
if __name__=='__main__':
    suite=unittest.TestSuite()#定义一个测试套件
    suite.addTest(TestCalc('test_pass_case'))#测试套件中添加测试用例
    suite.addTest(TestCalc('test_a'))
    suite.addTest(TestCalc('test_fail_case'))
    f=open('report.html','wb')#打开一个测试报告的文件
    runner=HTMLTestRunner.HTMLTestRunner(stream=f,title='wei测试结果',description='描述')#生成执行用例的对象
    runner.run(suite)#执行测试套件
import unittest,HTMLTestRunner
def calc(x,y):
    return x+y
class TestCalc(unittest.TestCase):
    def test_pass_case(self):
        '''这个是通过测试用例'''
        print('这个是通过测试用例!')
        res=calc(1,2)
        self.assertEqual(3,res)
        self.assertNotEqual(4,res)#返回的结果不一样
    def setUp(self):
        #每个用例运行之前都会执行它
        print('我是setup')
    def tearDown(self):
        #每个用例运行完之后都会执行它
        print('我是tearDown')
    @classmethod
    def setUpClass(cls):
        #所有的用例执行之前执行一次
        print('我是setupClass')
    @classmethod
    def tearDownClass(cls):
        #所有用例都运行完之后执行一次
        print('我是tearDownClass')
    def test_a(self):
        '''这个是普通的测试用例'''
        print('a')
    def test_fail_case(self):
        '''这个是失败的测试用例'''
        print('这个是失败的测试用例')
        res=calc(3,4)
        self.assertEqual(8,res)
    def test_haha(self):
        '''哈哈哈'''
        self.assertEqual(1,2)
if __name__=='__main__':
    suite=unittest.TestSuite()#定义一个测试套件
    suite.addTest(unittest.makeSuite(TestCalc))#这个类里面的所有测试用例
    f=open('report.html','wb')#打开一个测试报告的文件
    runner=HTMLTestRunner.HTMLTestRunner(stream=f,title='wei测试结果',description='描述')#生成执行用例的对象
    runner.run(suite)#执行测试套件

BeautifulReport: The same as HTMLTestRunner, but the report generated is more beautiful and detailed. Unzip the BeautifulReport file and copy it to External Libraries—>site-packages in pycharm

import unittest
from BeautifulReport import BeautifulReport
def calc(x,y):
    return x+y
class TestCalc(unittest.TestCase):
    def test_pass_case(self):
        '''这个是通过测试用例'''
        print('这个是通过测试用例!')
        res=calc(1,2)
        self.assertEqual(3,res)
        self.assertNotEqual(4,res)#返回的结果不一样
    def setUp(self):
        #每个用例运行之前都会执行它
        print('我是setup')
    def tearDown(self):
        #每个用例运行完之后都会执行它
        print('我是tearDown')
    @classmethod
    def setUpClass(cls):
        #所有的用例执行之前执行一次
        print('我是setupClass')
    @classmethod
    def tearDownClass(cls):
        #所有用例都运行完之后执行一次
        print('我是tearDownClass')
    def test_a(self):
        '''这个是普通的测试用例'''
        print('a')
    def test_fail_case(self):
        '''这个是失败的测试用例'''
        print('这个是失败的测试用例')
        res=calc(3,4)
        self.assertEqual(8,res)
    def test_haha(self):
        '''哈哈哈'''
        self.assertEqual(1,2)
if __name__=='__main__':
    suite=unittest.TestSuite()#定义一个测试套件
    suite.addTest(unittest.makeSuite(TestCalc))#这个类里面的所有测试用例
    result=BeautifulReport(suite)
    result.report(filename='xiaoReport',description='描述',log_path='.')

Run cases that are not in the same .py file:

cases:同一个目录下的文件夹,包含三个python文件 test_buy,test_login,test_reg
import unittest

class TestBuy(unittest.TestCase):
    def test_a(self):
        self.assertEqual(1,1)

    def test_b(self):
        self.assertEqual(1,2)

class TestBuy2(unittest.TestCase):
    def test_buy_phone(self):
        self.assertEqual(1,1)



import unittest

class TestLogin(unittest.TestCase):
    def test_a(self):
        self.assertEqual(1,1)

    def test_b(self):
        self.assertEqual(1,2)


import unittest
class TestReg(unittest.TestCase):
    def test_a(self):
        self.assertEqual(1,1)

    def test_b(self):
        self.assertEqual(1,2)

class TestReg2(unittest.TestCase):
    def test_buy_phone(self):
        self.assertEqual(1,1)

The run_all_cases.py file tests to execute the above test cases:

import unittest
from BeautifulReport import BeautifulReport
suite=unittest.TestSuite()
all_case=unittest.defaultTestLoader.discover('cases','test_*.py')
[suite.addTest(case) for case in all_case]
result=BeautifulReport(suite)
result.report(filename='Bea测试报告',description='描述',log_path='.')

In subsequent continuous integration, Jenkins will be used to automatically run the code, but Jenkins does not recognize test reports in html format. So it is necessary to generate a test report in a format that Jenkins recognizes, xml format. All you need to use the xmlrunner module, just pip install xmlrunner directly:

1 import unittest
2 import xmlrunner#为了以后给Jenkins看
3 suite=unittest.TestSuite()
4 all_case=unittest.defaultTestLoader.discover('cases','test_*.py')
5 # for case in all_case:
6 #     suite.addTest(case)
7 [suite.addTest(case) for case in all_case]#列表生成式,和上面代码是一样的
8 runner=xmlrunner.XMLTestRunner('.')#这是是为了产生报告给Jenkins看
#runner=xmlrunner.XMLTestRunner(output='report1')#指定生成目录
9 runner.run(suite)#运行用例

insert image description here
If you are interested in software testing, want to know more about testing knowledge, solve testing problems, and get started guidance to help you solve the confusion encountered in testing, we have technical experts here. If you are looking for a job or have just graduated from school, or have already worked but often feel that it is very difficult, feel that you are not good enough in the test, want to continue your studies, or want to change careers and are afraid that you will not be able to learn, you can join us insert image description here
. Get the latest software testing factory interview materials and Python automation, interface, framework construction learning materials!

Guess you like

Origin blog.csdn.net/qq_42434318/article/details/113618341