可怜的宾馆,可怜得像被12月的冷雨淋湿的一条三只腿的黑狗。——《舞舞舞》
?? ?? ?? \;\\\;\\\;
要么 test_前缀 在前,要么 _test后缀 在后!
#test_1.py
def test_1():
name='aa'
assert 'bb'==name
def test_2():
name='a'
assert 'bcb'==name
def test_3():
a = 1
assert a == 2
def test_4():
a = 4
assert a == 2
assert 'a' in 'abc'
assert 'a' not in 'abc'
assert 'a' is not True
assert 'a' is False
右击可以单独运行某个函数,看看哪个错了!
class TestTint:
def test_5(self):
a = 1
assert a == 2
def test_6(self):
a = 1
assert a == 2
def test_7(self):
a = 1
assert a == 2
?? ?? ?? \;\\\;\\\;
如果是按pytest格式的文件名,但是内容不是测试的话,那么会出现(没有发现测试)
#test_calc.py
a = 1
b = 2
print(a + b)
??
??
??
\;\\\;\\\;
import pytest
@pytest.fixture(scope='function')
def fixture1():
print('前置步骤1')
return 4
@pytest.fixture(scope='function')
def fixture2():
print('前置步骤2')
return 2
@pytest.fixture(scope='function',autouse=True)
def fixture3():
print('前置步骤3')
return 2
def test_1(fixture1,fixture2):
assert fixture1 == 2
assert fixture2 == 2
def test_2(fixture3):
assert fixture3 == 2
if __name__ == '__main__':
pytest.main()
可以在测试函数的位置,右击运行test_1或test_2函数
?? ?? ?? \;\\\;\\\;
关于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(scope='class')
def driver():
driver = webdriver.Edge(executable_path=r"C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Scripts\msedgedriver.exe")
#C:\ProgramData\anaconda3\Scripts\msedgedriver.exe
return driver
class TestSpider:
# fixture函数作为形参
def test_baidu(self, driver):
driver.get('https://www.baidu.com/')
title = driver.title
url = driver.current_url
text = driver.find_element(By.CSS_SELECTOR, "a[href*='news.baidu.com']").text
button = 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