[基础问题]:在使用pycharm运行用例时,参数化引用一个yaml文件时一直error提示找不到该文件,但是使用命令行pytest执行时就可以通过

代码:


import pytest

from pycode_demo.python_dev_code.calculator import Cal
from pycode_demo.test_cal import get_testdatas

@pytest.fixture(scope="class")
def get_cal():
    print("获取计算机实例")
    cal = Cal()
    return cal


class TestCal1:
    @pytest.mark.parametrize("a,b,expect",
                             get_testdatas("./pycode_demo/test_counter.yml")[0])
    def test_add(self, get_cal, a, b, expect):
        result1 = get_cal.addnum(a, b)
        assert result1 == expect


运行日志信息:

Testing started at 16:00 ...
"E:\python code\pytest_demo\env\Scripts\python.exe" "E:\PyCharm 2018.3.6\helpers\pycharm\_jb_pytest_runner.py" --path "E:/python code/pytest_demo/pycode_demo/test_cal_new.py"
Launching pytest with arguments E:/python code/pytest_demo/pycode_demo/test_cal_new.py in E:\python code\pytest_demo\pycode_demo

============================= test session starts =============================
platform win32 -- Python 3.6.8, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- E:\python code\pytest_demo\env\Scripts\python.exe
cachedir: .pytest_cache
rootdir: E:\python code\pytest_demo, configfile: pytest.ini
plugins: allure-pytest-2.9.43
collecting ... 
pycode_demo/test_cal_new.py:None (pycode_demo/test_cal_new.py)
test_cal_new.py:5: in <module>
    from pycode_demo.test_cal import get_testdatas
<frozen importlib._bootstrap>:971: in _find_and_load
    ???
<frozen importlib._bootstrap>:955: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:665: in _load_unlocked
    ???
..\env\lib\site-packages\_pytest\assertion\rewrite.py:170: in exec_module
    exec(co, module.__dict__)
test_cal.py:16: in <module>
    class TestCal:
test_cal.py:31: in TestCal
    get_testdatas("./pycode_demo/test_counter.yml")[0])
test_cal.py:7: in get_testdatas
    with open(path, encoding="utf-8") as num:
E   FileNotFoundError: [Errno 2] No such file or directory: './pycode_demo/test_counter.yml'
collected 0 items / 1 error

=================================== ERRORS ====================================
________________ ERROR collecting pycode_demo/test_cal_new.py _________________
test_cal_new.py:5: in <module>
    from pycode_demo.test_cal import get_testdatas
<frozen importlib._bootstrap>:971: in _find_and_load
    ???
<frozen importlib._bootstrap>:955: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:665: in _load_unlocked
    ???
..\env\lib\site-packages\_pytest\assertion\rewrite.py:170: in exec_module
    exec(co, module.__dict__)
test_cal.py:16: in <module>
    class TestCal:
test_cal.py:31: in TestCal
    get_testdatas("./pycode_demo/test_counter.yml")[0])
test_cal.py:7: in get_testdatas
    with open(path, encoding="utf-8") as num:
E   FileNotFoundError: [Errno 2] No such file or directory: './pycode_demo/test_counter.yml'
============================== warnings summary ===============================
..\env\lib\site-packages\_pytest\config\__init__.py:1233
  E:\python code\pytest_demo\env\lib\site-packages\_pytest\config\__init__.py:1233: PytestConfigWarning: Unknown config option: python_function
  
    self._warn_or_fail_if_strict(f"Unknown config option: {key}\n")

-- Docs: https://docs.pytest.org/en/stable/warnings.html
=========================== short test summary info ===========================
ERROR test_cal_new.py - FileNotFoundError: [Errno 2] No such file or director...
!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
========================= 1 warning, 1 error in 0.12s =========================
Process finished with exit code 0

提示未找到的文件地址和代码文件在一个目录下,使用pytest命令行执行正常,使用pycharm运行就报错。

贴下你代码所在目录和yaml文件所在目录看看


都在同一个目录下呢

既然这样子的话这个 pycode_demo/ 可以去掉

我好像找到原因了,是因为这里的路径需要使用绝对路径,但我用的相对路径,我拼接了一下路径就可以正常执行了。非常感谢您帮我看问题

关闭