【问题标题】:How to run test with "pytest" command如何使用“pytest”命令运行测试
【发布时间】:2020-02-04 11:13:09
【问题描述】:

我在我的测试文件中导入了 pytest 并安装了 allure。我为 PATH 添加了魅力。我将目录更改为终端中的测试文件,当我使用此命令“pytest -v -s test_1.py”运行测试时,会发生此错误

Traceback (most recent call last):
  File "c:\users\user\appdata\local\programs\python\python38-32\lib\runpy.py", line 192, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "c:\users\user\appdata\local\programs\python\python38-32\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\user\AppData\Local\Programs\Python\Python38-32\Scripts\pytest.exe\__main__.py", line 7, in <module>
  File "c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\_pytest\config\__init__.py", line 73, in main
    config = _prepareconfig(args, plugins)
  File "c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\_pytest\config\__init__.py", line 223, in _prepar
econfig
    return pluginmanager.hook.pytest_cmdline_parse(
  File "c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\pluggy\hooks.py", line 286, in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
  File "c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\pluggy\manager.py", line 93, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\pluggy\manager.py", line 84, in <lambda>
    self._inner_hookexec = lambda hook, methods, kwargs: hook.multicall(
  File "c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\pluggy\callers.py", line 203, in _multicall
    gen.send(outcome)
  File "c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\_pytest\helpconfig.py", line 89, in pytest_cmdlin
e_parse
    config = outcome.get_result()
  File "c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\pluggy\callers.py", line 80, in get_result
    raise ex[1].with_traceback(ex[2])
  File "c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\pluggy\callers.py", line 187, in _multicall
    res = hook_impl.function(*args)
  File "c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\_pytest\config\__init__.py", line 794, in pytest_
cmdline_parse
    self.parse(args)
  File "c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\_pytest\config\__init__.py", line 1000, in parse
    self._preparse(args, addopts=addopts)
  File "c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\_pytest\config\__init__.py", line 948, in _prepar
se
    self.pluginmanager.load_setuptools_entrypoints("pytest11")
  File "c:\users\user\appdata\local\programs\python\python38-32\lib\site-packages\pluggy\manager.py", line 299, in load_setuptools_
entrypoints
    plugin = ep.load()
  File "c:\users\user\appdata\local\programs\python\python38-32\lib\importlib\metadata.py", line 75, in load
    module = import_module(match.group('module'))
  File "c:\users\user\appdata\local\programs\python\python38-32\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 970, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'allure.pytest_plugin'; 'allure' is not a package

【问题讨论】:

  • 请考虑修改您在此问题中发布的代码示例。就目前而言,它的格式和范围使我们很难为您提供帮助;这是一个great resource,可以帮助您开始。 -1,不要走错路。否决票是我们在这里指出内容问题的方式;改进您的格式和代码示例,我(或有人会)很乐意将其还原。祝你的代码好运!
  • 您好,请在错误前插入您的测试代码(或简化版)
  • 测试代码无所谓,因为当我在cmd中只运行pytest命令时,也会出现同样的错误。
  • 请阅读How to Ask,然后阅读edit您的问题,并将您的代码包含为minimal reproducible example。目前,这可能会因为不清楚而被关闭。

标签: python selenium testing allure


【解决方案1】:

这是在打印“B”的测试函数之前运行打印“A”的 conftest.py 函数的示例。

cd 到父目录,在本例中为 py_tests 并运行。

pytest -s -v

输出是:

A
setting up
B
PASSED

带目录结构:

py_tests
 -conftest.py
 -tests
  --tests.py

文件:

conftest.py

import pytest

@pytest.fixture(scope="function")
def print_one():
    print("\n")
    print("A")

test.py

import pytest

class Testonething:

    @pytest.fixture(scope="function", autouse=True)
    def setup(self, print_one):
        print("setting up")

    def test_one_thing(self):
        print("B")
        assert True

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    • 2020-07-02
    • 1970-01-01
    • 1970-01-01
    • 2014-04-23
    • 2022-08-19
    相关资源
    最近更新 更多