fixture参数一直报错fixture 'username' not found

问题

conftest.py

import pytest
from page.loginPage import LoginPage

# 登录ITS
# 可以传入登录的账号名称
# username = 'chenyao007'
@pytest.fixture(scope='class')
def login_ITS(username):
    login_ITS = LoginPage()
    login = login_ITS.login_ITS(username)
    # 鉴权信息
    authorization = login[2]+' '+login[3]
    # 把鉴权信息放入header中共享给所有接口
    login_ITS.session.headers.update({'Authorization':authorization})

fcFirstPage_test.py

from page.fcFirstPage import FCfirstPage
from loguru import logger
import pytest

class TestFCfirstPage():
    fcFirstPage = FCfirstPage()

    username=['machao341']
    # 获取座席信息
    pytest.mark.parametrize('login_ITS',
                            username,
                            indirect= True)
    def test_getUserInfo(self,login_ITS):
        response = self.fcFirstPage.getUserInfo()
        logger.info(response)
        logger.info(response['data']['tmrId'])
        assert response['data']['tmrId'] == 'MACHAO341'

报错信息

Testing started at 15:12 ...
D:\workspace\PAC_API_Test\venv\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2018.3.7\helpers\pycharm\_jb_pytest_runner.py" --target fcFirstPage_test.py::TestFCfirstPage
Launching pytest with arguments fcFirstPage_test.py::TestFCfirstPage in D:\workspace\PAC_API_Test\test_cases

============================= test session starts =============================
platform win32 -- Python 3.7.0, pytest-7.1.2, pluggy-1.0.0
rootdir: D:\workspace\PAC_API_Test, configfile: pytest.inicollected 1 item

fcFirstPage_test.py E
test setup failed
file D:\workspace\PAC_API_Test\test_cases\fcFirstPage_test.py, line 13
      def test_getUserInfo(self,login_ITS):
file D:\workspace\PAC_API_Test\test_cases\conftest.py, line 18
  @pytest.fixture(scope='class')
  def login_ITS(username):
E       fixture 'username' not found
>       available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, login_ITS, login_wizard, 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.

D:\workspace\PAC_API_Test\test_cases\conftest.py:18
                                                    [100%]

=================================== ERRORS ====================================
_____________ ERROR at setup of TestFCfirstPage.test_getUserInfo ______________
file D:\workspace\PAC_API_Test\test_cases\fcFirstPage_test.py, line 13
      def test_getUserInfo(self,login_ITS):
file D:\workspace\PAC_API_Test\test_cases\conftest.py, line 18
  @pytest.fixture(scope='class')
  def login_ITS(username):
E       fixture 'username' not found
>       available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, login_ITS, login_wizard, 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.

D:\workspace\PAC_API_Test\test_cases\conftest.py:18
=========================== short test summary info ===========================
ERROR fcFirstPage_test.py::TestFCfirstPage::test_getUserInfo
============================== 1 error in 0.03s ===============================
Process finished with exit code 0

环境

python3

解决办法


fixture函数只能传入参数request,要实现不同用户登录,可以使用如下方式

test_user_data = [{'user': 'machao341', 'pwd': '2gynz30J'}, {'user': 'chenyao007', 'pwd': '2gynz30J'}]
@pytest.fixture(autouse=True,scope='session',params=test_user_data)
def login(request):
    user = request.param['user']
    pwd = request.param['pwd']
    yield  LoginPage().login(user, pwd)

看报错, 就是fixture 的方法login_ITS(username) 没有取到 username, 把username的值写到 fixture 里面去吧. 直接传入试试.

直接写在fixture里是在conftest.py文件里传入么?如果不同用例需要不同的username,需要在用例里传入,可以实现么?

import pytest
from page.loginPage import LoginPage

# 登录ITS
# 可以传入登录的账号名称
# username = 'chenyao007'
@pytest.fixture(scope='class')
def login_ITS():
    login_ITS = LoginPage()
    username=['machao341']
    login = login_ITS.login_ITS(username)
    # 鉴权信息
    authorization = login[2]+' '+login[3]
    # 把鉴权信息放入header中共享给所有接口
    login_ITS.session.headers.update({'Authorization':authorization})

大概是这个意思.

还没有尝试过不同用户登录都写入到fixture里面, 应该也可以这么做.

感觉用例里面的话, 直接写在setup class , 里面要方便点吧. 直接取到对应的测试数据. 然后跑当前用例.