从命令行运行python硒脚本时,我遇到了夹具错误

时间:2019-07-17 04:48:41

标签: python selenium pytest

我正在使用selenium和python创建一个自动化框架,因此在conftest.py文件中我使用了fixture,但是当我通过cmd行运行测试用例时却出现错误

我已经一遍又一遍地检查了,但我没有得到此错误的根本原因

此代码在我的conftest.py文件下

import pytest
@pytest.fixture(scope="class")
def test_setup(request):
    from selenium import webdriver

    browser =  request.config.getoption("--browser")
    if browser == "chrome":
        driver = webdriver.Chrome(executable_path="/home/akash/PycharmProjects/AutomationFramework/drivers/chromedriver")     
        request.cls.driver = driver
        yield
        driver.quit()

下面是测试用例文件

@pytest.mark.usefixtures("test_setup")
class TestLogin:

    def test_login(self):
        driver = self.driver
        driver.get(utils.URL)

当我以python -m pytest命令运行时,出现以下错误 但是,它应该已经打开浏览器并完成工作

我得到的错误

rootdir: /home/akash/PycharmProjects/AutomationFramework
plugins: html-1.21.1, metadata-1.8.0
collected 2 items                                                                                                                                    

tests/login_test.py EE                                                                                                                         [100%]

======================================================================= ERRORS =======================================================================
_______________________________________________________ ERROR at setup of TestLogin.test_login _______________________________________________________

cls = <class '_pytest.runner.CallInfo'>, func = <function call_runtest_hook.<locals>.<lambda> at 0x7f8e17b4ed08>, when = 'setup'
reraise = (<class '_pytest.outcomes.Exit'>, <class 'KeyboardInterrupt'>)

    @classmethod
    def from_call(cls, func, when, reraise=None):
        #: context of invocation: one of "setup", "call",
        #: "teardown", "memocollect"
        start = time()
        excinfo = None
        try:
>           result = func()

venv/lib/python3.6/site-packages/_pytest/runner.py:220: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
venv/lib/python3.6/site-packages/_pytest/runner.py:192: in <lambda>
    lambda: ihook(item=item, **kwds), when=when, reraise=reraise
venv/lib/python3.6/site-packages/pluggy/hooks.py:289: in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
venv/lib/python3.6/site-packages/pluggy/manager.py:87: in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
venv/lib/python3.6/site-packages/pluggy/manager.py:81: in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
venv/lib/python3.6/site-packages/_pytest/runner.py:110: in pytest_runtest_setup
    item.session._setupstate.prepare(item)
venv/lib/python3.6/site-packages/_pytest/runner.py:359: in prepare
    col.setup()
venv/lib/python3.6/site-packages/_pytest/python.py:1455: in setup
    fixtures.fillfixtures(self)
venv/lib/python3.6/site-packages/_pytest/fixtures.py:288: in fillfixtures
    request._fillfixtures()
venv/lib/python3.6/site-packages/_pytest/fixtures.py:461: in _fillfixtures
    item.funcargs[argname] = self.getfixturevalue(argname)
venv/lib/python3.6/site-packages/_pytest/fixtures.py:471: in getfixturevalue
    return self._get_active_fixturedef(argname).cached_result[0]
venv/lib/python3.6/site-packages/_pytest/fixtures.py:494: in _get_active_fixturedef
    self._compute_fixture_value(fixturedef)
venv/lib/python3.6/site-packages/_pytest/fixtures.py:575: in _compute_fixture_value
    fixturedef.execute(request=subrequest)
venv/lib/python3.6/site-packages/_pytest/fixtures.py:883: in execute
    return hook.pytest_fixture_setup(fixturedef=self, request=request)
venv/lib/python3.6/site-packages/pluggy/hooks.py:289: in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
venv/lib/python3.6/site-packages/pluggy/manager.py:87: in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
venv/lib/python3.6/site-packages/pluggy/manager.py:81: in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
venv/lib/python3.6/site-packages/_pytest/fixtures.py:923: in pytest_fixture_setup
    result = call_fixture_func(fixturefunc, request, kwargs)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

fixturefunc = <function test_setup at 0x7f8e17b61f28>, request = <SubRequest 'test_setup' for <Function test_login>>
kwargs = {'request': <SubRequest 'test_setup' for <Function test_login>>}

    def call_fixture_func(fixturefunc, request, kwargs):
        yieldctx = is_generator(fixturefunc)
        if yieldctx:
            it = fixturefunc(**kwargs)
>           res = next(it)
E           StopIteration

venv/lib/python3.6/site-packages/_pytest/fixtures.py:779: StopIteration
______________________________________________________ ERROR at setup of TestLogin.test_logout _______________________________________________________

cls = <class '_pytest.runner.CallInfo'>, func = <function call_runtest_hook.<locals>.<lambda> at 0x7f8e179ba048>, when = 'setup'
reraise = (<class '_pytest.outcomes.Exit'>, <class 'KeyboardInterrupt'>)

    @classmethod
    def from_call(cls, func, when, reraise=None):
        #: context of invocation: one of "setup", "call",
        #: "teardown", "memocollect"
        start = time()
        excinfo = None
        try:
>           result = func()

venv/lib/python3.6/site-packages/_pytest/runner.py:220: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
venv/lib/python3.6/site-packages/_pytest/runner.py:192: in <lambda>
    lambda: ihook(item=item, **kwds), when=when, reraise=reraise
venv/lib/python3.6/site-packages/pluggy/hooks.py:289: in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
venv/lib/python3.6/site-packages/pluggy/manager.py:87: in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
venv/lib/python3.6/site-packages/pluggy/manager.py:81: in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
venv/lib/python3.6/site-packages/_pytest/runner.py:110: in pytest_runtest_setup
    item.session._setupstate.prepare(item)
venv/lib/python3.6/site-packages/_pytest/runner.py:359: in prepare
    col.setup()
venv/lib/python3.6/site-packages/_pytest/python.py:1455: in setup
    fixtures.fillfixtures(self)
venv/lib/python3.6/site-packages/_pytest/fixtures.py:288: in fillfixtures
    request._fillfixtures()
venv/lib/python3.6/site-packages/_pytest/fixtures.py:461: in _fillfixtures
    item.funcargs[argname] = self.getfixturevalue(argname)
venv/lib/python3.6/site-packages/_pytest/fixtures.py:471: in getfixturevalue
    return self._get_active_fixturedef(argname).cached_result[0]
venv/lib/python3.6/site-packages/_pytest/fixtures.py:494: in _get_active_fixturedef
    self._compute_fixture_value(fixturedef)
venv/lib/python3.6/site-packages/_pytest/fixtures.py:575: in _compute_fixture_value
    fixturedef.execute(request=subrequest)
venv/lib/python3.6/site-packages/_pytest/fixtures.py:874: in execute
    raise val.with_traceback(tb)
venv/lib/python3.6/site-packages/_pytest/fixtures.py:923: in pytest_fixture_setup
    result = call_fixture_func(fixturefunc, request, kwargs)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

fixturefunc = <function test_setup at 0x7f8e17b61f28>, request = <SubRequest 'test_setup' for <Function test_login>>
kwargs = {'request': <SubRequest 'test_setup' for <Function test_login>>}

    def call_fixture_func(fixturefunc, request, kwargs):
        yieldctx = is_generator(fixturefunc)
        if yieldctx:
            it = fixturefunc(**kwargs)
>           res = next(it)
E           StopIteration

venv/lib/python3.6/site-packages/_pytest/fixtures.py:779: StopIteration

1 个答案:

答案 0 :(得分:1)

我得到的解决方案只是因为缩进,所以在我写

import pytest
@pytest.fixture(scope="class")
def test_setup(request):
    from selenium import webdriver

    browser =  request.config.getoption("--browser")
    if browser == "chrome":
        driver = webdriver.Chrome(executable_path="/home/akash/PycharmProjects/AutomationFramework/drivers/chromedriver")     
    request.cls.driver = driver
    yield
    driver.quit()

所以在上面我要设置可执行路径的那一行下面的语句中,下一行是针对我创建的函数的,因此这些语句位于该函数的下面,而不是if语句,这就是为什么它导致上述错误的原因。