辽宁鲲鹏建设集团网站,网站建设分几种类型,wordpress怎么下载文件,做网站上传服务器前言
可怜的宾馆#xff0c;可怜得像被12月的冷雨淋湿的一条三只腿的黑狗。——《舞舞舞》 \;\\\;\\\; 目录 前言test_1或s_test格式非测试文件pytest.fixture()装饰器pytestselenium test_1或s_test格式
要么 test_前缀 在前#xff0c;要么 _test后缀 在后#xff01;
…前言
可怜的宾馆可怜得像被12月的冷雨淋湿的一条三只腿的黑狗。——《舞舞舞》 \;\\\;\\\; 目录 前言test_1或s_test格式非测试文件pytest.fixture()装饰器pytestselenium test_1或s_test格式
要么 test_前缀 在前要么 _test后缀 在后
#test_1.py
def test_1():nameaaassert bbnamedef test_2():nameaassert bcbnamedef test_3():a 1assert a 2def test_4():a 4assert a 2assert a in abcassert a not in abcassert a is not Trueassert a is False右击可以单独运行某个函数看看哪个错了
class TestTint:def test_5(self):a 1assert a 2def test_6(self):a 1assert a 2def test_7(self):a 1assert a 2\;\\\;\\\;
非测试文件
如果是按pytest格式的文件名但是内容不是测试的话那么会出现没有发现测试
#test_calc.py
a 1
b 2
print(a b)\;\\\;\\\;
pytest.fixture()装饰器
import pytestpytest.fixture(scopefunction)
def fixture1():print(前置步骤1)return 4pytest.fixture(scopefunction)
def fixture2():print(前置步骤2)return 2pytest.fixture(scopefunction,autouseTrue)
def fixture3():print(前置步骤3)return 2def test_1(fixture1,fixture2):assert fixture1 2assert fixture2 2def test_2(fixture3):assert fixture3 2if __name__ __main__:pytest.main()可以在测试函数的位置右击运行test_1或test_2函数 \;\\\;\\\;
pytestselenium
关于selenium使用的edge驱动器版本要和电脑上装的edge版本一致
pytest类要以Test为前缀
#test_f.py
from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
import pytest# 前置方法
pytest.fixture(scopeclass)
def driver():driver webdriver.Edge(executable_pathrC:\Users\Administrator\AppData\Local\Programs\Python\Python311\Scripts\msedgedriver.exe)#C:\ProgramData\anaconda3\Scripts\msedgedriver.exereturn driverclass TestSpider:# fixture函数作为形参def test_baidu(self, driver):driver.get(https://www.baidu.com/)title driver.titleurl driver.current_urltext driver.find_element(By.CSS_SELECTOR, a[href*news.baidu.com]).textbutton driver.find_element(By.ID, su).get_attribute(value)# 检测assert title 百度一下你就知道assert url https://www.baidu.com/assert text 新闻assert button 百度一下# sleep(3)
# driver.close()if __name__ __main__:pytest.main()运行命令 pytest test_f.py