這是我的 conf.py、baseClass.py 和實際測驗檔案 (test_e2e.py)。你能檢查一下,讓我知道我的代碼有什么問題嗎?看起來一切都很好,但我的測驗仍然沒有運行并顯示“未找到夾具”錯誤。
conf.py 檔案
import pytest
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
@pytest.fixture(scope="class")
def setup(request):
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--start-maximized")
# chrome_options.add_argument("headless")
chrome_options.add_argument("--ignore-certificate-errors")
ser = Service("C://chromedriver.exe")
driver = webdriver.Chrome(service=ser,options=chrome_options)
driver.get('https://google.com')
request.cls.driver = driver
yield
driver.close()
基類.py
import pytest
@pytest.mark.usefixtures("setup")
class BaseClass:
pass
主要測驗檔案
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from utilities.BaseClass import BaseClass
class TestOne(BaseClass):
def test_e2e(self):
self.driver.find_element(By.CSS_SELECTOR, "a[href*='shop']").click()
cards = self.driver.find_elements(By.CSS_SELECTOR, ".card-title a")
i = -1
for card in cards:
i = i 1
cardText = card.text
print(cardText)
if cardText == "Blackberry":
self.driver.find_element(By.CSS_SELECTOR, ".card-footer button")[i].click()
執行后我得到的錯誤
============================= test session starts =============================
收集...收集了 1 個專案
test_e2e.py::TestOne::test_e2e 錯誤 [100%] 測驗設定失敗檔案 C:\Users\Admin\Desktop\pythonselframework\tests\test_e2e.py, line 8 def test_e2e(self): E fixture 'setup' not found
available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory use 'pytest --fixtures [testpath]' for help on them.
我希望我的硒測驗能夠順利進行。我在我的檔案中沒有發現任何錯誤,但是當我運行我的測驗檔案時,它仍然顯示錯誤。
uj5u.com熱心網友回復:
Pytest 使用單個檔案進行夾具發現,conftest.py檔案。然后,您應該將夾具宣告存盤在conftest.py檔案中(而不是conf.py您宣告的 a)。此檔案應存盤在測驗根檔案夾中。
更多關于 conftest 檔案:這里
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/451725.html
上一篇:Java模型類中的封裝
