Получение прибора не найдено в pytest - PullRequest
0 голосов
/ 28 февраля 2019

Я получаю следующую ошибку при запуске pytest с использованием следующего кода, я не могу понять, что не так, пожалуйста, найдите ниже фрагменты кода.

Выход консоли:

================================================= test session starts =================================================
platform win32 -- Python 3.7.2, pytest-4.2.0, py-1.7.0, pluggy-0.8.1
rootdir: D:\Workspace\AutomationProject, inifile:
plugins: cov-2.6.1, allure-pytest-2.5.5
collected 1 item

tests\pages\test.py E                                                                                            [100%]

======================================================= ERRORS ========================================================
__________________________________________ ERROR at setup of test.test_test ___________________________________________
file D:\Workspace\AutomationProject\tests\pages\test.py, line 5
      def test_test(self):
E       fixture 'web_driver' not found
>       available fixtures: _UnitTestCase__pytest_class_setup, cache, capfd, capfdbinary, caplog, capsys, capsysbinary, cov, doctest_namespace, monkeypatch, no_cover, pytestconfig, record_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
>       use 'pytest --fixtures [testpath]' for help on them.

D:\Workspace\AutomationProject\tests\pages\test.py:5
=============================================== 1 error in 0.12 seconds ===============================================

Мой базовый класс содержит следующий код:

from selenium import webdriver
import pytest
import unittest

@pytest.fixture(scope="class")
def web_driver(request):
    driver = webdriver.Chrome("C:/chromedriver.exe")
    request.cls.driver = driver
    yield
    web_driver.close()


@pytest.mark.usefixtures("web_driver")
class Base(unittest.TestCase):
    '''
    This fixture contains the set up and tear down code for each test.

    '''
    pass

, а тестовый класс содержит следующий код:

from core.web.Base import Base

class test(Base):

    def test_test(self):
        self.driver.get("http://google.com")

Тестовое устройство web_driver все еще получает ошибку, не найденную!

1 Ответ

0 голосов
/ 28 февраля 2019

web_driver() определен вне Base области видимости, поэтому он невидим для usefixtures, поскольку является частью test области видимости.Вы можете переместить его в файл оспаривания , но ИМХО, лучшее решение - переместить web_driver внутрь Base

@pytest.mark.usefixtures("web_driver")
class Base(unittest.TestCase):

    @pytest.fixture(scope="class")
    def web_driver(self, request):
        driver = webdriver.Chrome("C:/chromedriver.exe")
        request.cls.driver = driver
        yield
        driver.close()

В качестве примечания следует указать driver.close(),не web_driver.close()

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...