unittest用例前后置
import unittest
def setUpModule():
print("模块级别的夹具开始")
def tearDownModule():
print("模块级别的夹具结束")
class Test1(unittest.TestCase):
@classmethod
def setUpClass(cls):
print("\n在每个类之前执行一次,如创建数据库连接,生成日志对象")
@classmethod
def tearDownClass(cls):
print("在每个类之后运行,如关闭数据库连接,销毁日志对象")
def setUp(self):
print("\n用例开始前运行,打开浏览器、关闭网页等")
def tearDown(self):
print("用例结束后运行,关闭浏览器等")
def test_01(self):
print("用例01")
def test_02(self):
print("用例02")
class Test2(unittest.TestCase):
def test_03(self):
print("用例03")
返回:
Testing started at 5:02 PM ...
/Users/dongshuai/opt/anaconda3/bin/python /Applications/PyCharm.app/Contents/helpers/pycharm/_jb_pytest_runner.py --path test.py
Launching py.test with arguments test.py in /Users/dongshuai/PycharmProjects/selenium2/test_case/.pytest_cache
============================= test session starts ==============================
platform darwin -- Python 3.8.5, pytest-6.2.3, py-1.9.0, pluggy-0.13.1
rootdir: /Users/dongshuai/PycharmProjects/selenium2/test_case/.pytest_cache
plugins: rerunfailures-9.1.1, assume-2.4.3, html-3.1.1, xdist-2.2.1, metadata-1.11.0, allure-pytest-2.8.40, ordering-0.6, forked-1.3.0collected 3 items
test.py 模块级别的夹具开始
在每个类之前执行一次,如创建数据库连接,生成日志对象
.
用例开始前运行,打开浏览器、关闭网页等
用例01
用例结束后运行,关闭浏览器等
.
用例开始前运行,打开浏览器、关闭网页等
用例02
用例结束后运行,关闭浏览器等
在每个类之后运行,如关闭数据库连接,销毁日志对象
.用例03
模块级别的夹具结束
[100%]
============================== 3 passed in 0.01s ===============================
Process finished with exit code 0
|