简体   繁体   English

如何使用“pytest”命令运行测试

[英]How to run test with "pytest" command

I imported pytest in my testing file and installed allure.我在我的测试文件中导入了 pytest 并安装了 allure。 I added allure to PATH.我为 PATH 添加了吸引力。 I changed the directory to my testing file in the terminal and when I am running my test with this command "pytest -v -s test_1.py" this error occurs我在终端中将目录更改为我的测试文件,当我使用此命令“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

Here is an example of running my conftest.py function that prints "A" before my test function that prints "B".这是运行我的 conftest.py 函数的示例,该函数在打印“B”的测试函数之前打印“A”。

cd to the parent directory, for this example it is py_tests and run. cd 到父目录,在这个例子中它是 py_tests 并运行。

pytest -s -v

The output is:输出是:

A
setting up
B
PASSED

With directory structure:使用目录结构:

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

Files:文件:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM