测试进阶课

UnitTest框架常用参数及用法

2019-04-25  本文已影响178人  Xyxtank

一、启动参数

python -m unittest XXX.py -c
#python -m unittest XXX.py -b
C:\WordCloudPro\mytest>python -m unittest test.py -b
..
----------------------------------------------------------------------
Ran 2 tests in 0.001s

OK

如果测试用例测试错误,那么会打印出其他的描述信息。

C:\WordCloudPro\mytest>python -m unittest test.py -b
F
Stdout:
预期结果为:2
这是实际结果:3
F
Stdout:
预期结果为:2
这是实际结果:3

======================================================================
FAIL: test_add_one (test.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\WordCloudPro\mytest\test.py", line 15, in test_add_one
    self.assertEqual(self.res,add_res)
AssertionError: 2 != 3

Stdout:
预期结果为:2
这是实际结果:3

代码如下:

import unittest

class MyTestCase(unittest.TestCase):
    def setUp(self):
        self.res = 2

    def add(self,a,b):
        return a + b

    def test_add_one(self):
        add_res = self.add(1,2)
        print("预期结果为:{0}".format(self.res))
        print("这是实际结果:{0}".format(add_res))

        self.assertEqual(self.res,add_res)

    def test_add_two(self):
        add_res = self.add(1,2)
        print("预期结果为:{0}".format(self.res))
        print("这是实际结果:{0}".format(add_res))

        self.assertEqual(self.res,add_res)


if __name__ == '__main__':
    unittest.main()

# python -m unittest XXX.py --locals
C:\WordCloudPro\mytest>python -m unittest test.py --locals
预期结果为:2
这是实际结果:3
F预期结果为:2
这是实际结果:3
F
======================================================================
FAIL: test_add_one (test.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\WordCloudPro\mytest\test.py", line 15, in test_add_one
    self.assertEqual(self.res,add_res)
    add_res = 3
    self = <test.MyTestCase testMethod=test_add_one>
AssertionError: 2 != 3

======================================================================
FAIL: test_add_two (test.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\WordCloudPro\mytest\test.py", line 22, in test_add_two
    self.assertEqual(self.res,add_res)
    add_res = 3
    self = <test.MyTestCase testMethod=test_add_two>
AssertionError: 2 != 3

----------------------------------------------------------------------
Ran 2 tests in 0.001s

FAILED (failures=2)
python -m unittest XXX.py -b --locals -f
python -m unittest -k 

二、测试发现

按照某种命名规则去找到需要执行的测试用例

#python -m unittest
C:\WordCloudPro\mytest>python -m unittest

------

Ran 0 tests in 0.000s

OK

当测试用例文件名改为test_one.py时,用刚才的命令就可以找到测试用例。

C:\WordCloudPro\mytest>python -m unittest
预期结果为:2
这是实际结果:3
F预期结果为:2
这是实际结果:3
F
======================================================================
FAIL: test_add_one (test_one.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\WordCloudPro\mytest\test_one.py", line 15, in test_add_one
    self.assertEqual(self.res,add_res)
AssertionError: 2 != 3

======================================================================
FAIL: test_add_two (test_one.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\WordCloudPro\mytest\test_one.py", line 22, in test_add_two
    self.assertEqual(self.res,add_res)
AssertionError: 2 != 3

----------------------------------------------------------------------
Ran 2 tests in 0.003s

FAILED (failures=2)

若测试用例的名称不是以test开头,那怎么才能正确的匹配到需要执行的测试用例呢?那就需要要到"-p"参数。比如现在要找的测试用例名称是“aaa.py”。这样就能方便的找到测试用例。

C:\WordCloudPro\mytest>python -m unittest discover -p "aa*.py"
预期结果为:2
这是实际结果:3
F预期结果为:2
这是实际结果:3
F
======================================================================
FAIL: test_add_one (aaa.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\WordCloudPro\mytest\aaa.py", line 15, in test_add_one
    self.assertEqual(self.res,add_res)
AssertionError: 2 != 3

======================================================================
FAIL: test_add_two (aaa.MyTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\WordCloudPro\mytest\aaa.py", line 22, in test_add_two
    self.assertEqual(self.res,add_res)
AssertionError: 2 != 3

----------------------------------------------------------------------
Ran 2 tests in 0.007s

FAILED (failures=2)

C:\WordCloudPro\mytest>

三、组织代码

测试可能存在的几种代码

测试夹具:就是测试用例执行之前的操作和执行过后的收尾操作。

在编写测试用例时,应该要遵循测试框架的规范,比如,setUp、tearDown方法不能遗漏。假设测试用例中没有tearDown结束方法,那么浏览器就会一直开着,导致其一直占用系统的资源。当测试用例很多的时候,这个时候若不能及时释放系统资源,那后果不可想象。

import unittest
from selenium import webdriver


path = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"

class MyTestCase(unittest.TestCase):

    def setUp(self):
        print("1、打开浏览器")
        self.driver = webdriver.Chrome(executable_path=path)
        self.url_baidu = "https://www.baidu.com/"
        self.url_163 = "https://www.163.com/"

    def test_add_baidu(self):
        self.driver.get(self.url_baidu)
        print("2、打开百度")

    def test_add_163(self):
        self.driver.get(self.url_163)
        print("3、打开网易")

    def tearDown(self):
        print('4、关闭浏览器')
        self.driver.quit()


if __name__ == '__main__':
    unittest.main()

结果为:

C:\WordCloudPro\mytest>python -m unittest discover -p "aa*.py"
1、打开浏览器

DevTools listening on ws://127.0.0.1:9354/devtools/browser/4c555182-3149-4d3f-b032-9ed59f0b9855
3、打开网易
4、关闭浏览器
.1、打开浏览器

DevTools listening on ws://127.0.0.1:9487/devtools/browser/dd17c342-9600-4b79-a7dc-a28ff0da09ff
[6204:2972:0425/152235.079:ERROR:ssl_client_socket_impl.cc(946)] handshake failed; returned -1, SSL error code 1, net_error -101
[6204:2972:0425/152235.118:ERROR:ssl_client_socket_impl.cc(946)] handshake failed; returned -1, SSL error code 1, net_error -101
2、打开百度
4、关闭浏览器
[6204:2972:0425/152237.060:ERROR:broker_win.cc(55)] Error reading broker pipe: 管道已结束。 (0x6D)
.
----------------------------------------------------------------------
Ran 2 tests in 17.456s

OK

若不想命令行终端测试结果显示太多信息,可以加上前面所学的"-b"缓冲输出。

C:\WordCloudPro\mytest>python -m unittest discover -p "aa*.py" -b

DevTools listening on ws://127.0.0.1:9653/devtools/browser/ddd4ebe1-e465-4cf4-a987-bac375d81c3e
.
DevTools listening on ws://127.0.0.1:9788/devtools/browser/22f61aea-3299-4b9b-9dd7-56c3d055941e
.
----------------------------------------------------------------------
Ran 2 tests in 18.100s

OK

这里会发现一个问题,那就是当测试用例很多的时候,执行每个测试用例都会打开一个浏览器,要执行完这些测试用例必定会花费大量的时间,此时就需要下面的方法,提高测试用例执行效率。

import unittest
from selenium import webdriver


path = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"

class MyTestCase(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        print("1、打开浏览器")
        cls.driver = webdriver.Chrome(executable_path=path)
        cls.url_baidu = "https://www.baidu.com/"
        cls.url_163 = "https://www.163.com/"

    def test_add_baidu(self):
        self.driver.get(self.url_baidu)
        print("2、打开百度")

    def test_add_163(self):
        self.driver.get(self.url_163)
        print("3、打开网易")

    @classmethod
    def tearDownClass(cls):
        print('4、关闭浏览器')
        cls.driver.quit()


if __name__ == '__main__':
    unittest.main()

这里发现执行测试用例只打开了一个浏览器,并在同一个浏览器同一个窗口中先打开了“网易”再打开“百度”,最后再关闭浏览器,这就避免了,每次执行一个测试用例时就会开启一个浏览器,耗时、耗资源。

C:\WordCloudPro\mytest>python -m unittest discover -p "aa*.py"
1、打开浏览器

DevTools listening on ws://127.0.0.1:10117/devtools/browser/77015695-753a-4034-b058-18d86586ce5e
3、打开网易
.2、打开百度
.4、关闭浏览器

----------------------------------------------------------------------
Ran 2 tests in 13.909s

OK

在命令行输入“pytest --tests-per-worker auto aaa.py”,可以发现同时弹出两个浏览器窗口,并行执行测试用例。注意执行pytest命令之前需要安装pytest相关库。这里只是介绍有这种方法,在执行测试用例时根据需要选择即可。

C:\WordCloudPro\mytest>pytest --tests-per-worker auto aaa.py
================================================= test session starts =================================================
platform win32 -- Python 3.6.4, pytest-4.4.1, py-1.8.0, pluggy-0.9.0
rootdir: C:\WordCloudPro\mytest
plugins: parallel-0.0.9, celery-4.3.0
collected 2 items
pytest-parallel: 1 worker (process), 2 tests per worker (threads)

DevTools listening on ws://127.0.0.1:10713/devtools/browser/afca947a-b9d9-4a93-95d6-bf51db7d72c5

DevTools listening on ws://127.0.0.1:10714/devtools/browser/ac3a6e2a-5092-4fbc-bac1-70c6d594e28b
..                                                                                                               [100%]
================================================== warnings summary ===================================================
aaa.py:5
  C:\WordCloudPro\mytest\aaa.py:5: DeprecationWarning: invalid escape sequence \P
    path = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"

-- Docs: https://docs.pytest.org/en/latest/warnings.html
======================================== 2 passed, 1 warnings in 14.01 seconds ========================================

上一篇下一篇

猜你喜欢

热点阅读