【问题标题】:pytest finds no testspytest 找不到任何测试
【发布时间】:2020-10-26 08:52:17
【问题描述】:

我在多个帖子中检查了这个问题,我认为我满足了所有要求,但 pytest 仍然没有找到任何测试。

我的测试类叫做 test_login.py

它的内容是:

class TestLogin:

    def test_login_success(self, driver, messages, log):
        login = LoginPage(driver)
        login.enter_email("Mark")
        login.enter_password("Passw0rd")
        login.click_login()
        log.info("Login clicked")

        assert messages['login_page_title'] == driver.title, \
            f"Expected: {messages['login_page_title']}\nActual: {driver.title}"

我从具有以下内容的 pytest.ini 的根目录运行它:

[pytest]
addopts = --strict-markers -v
markers =
    feature: Deep validation of the application

和一个 conftest.py ,其中定义了测试中使用的夹具。 项目中的每个目录都有一个__init__.py 文件

当我运行测试时,我得到:

================================================================ test session starts ================================================================
platform darwin -- Python 3.8.5, pytest-5.2.1, py-1.8.0, pluggy-0.13.0 -- /Users/mark/Python_Auto_Framework/com/companyname/venv/bin/python3
cachedir: .pytest_cache
rootdir: /Users/mark/Python_Auto_Framework, inifile: pytest.ini
plugins: testrail-e2e-2.0.0, xdist-1.30.0, bdd-3.2.1, forked-1.1.1, docker-compose-3.2.0, cov-2.8.1
collected 0 items                                                                                                                                   

=============================================================== no tests ran in 0.04s ===============================================================

当我尝试通过像这样直接引用它来运行测试时:

pytest com/companyname/Tests/test_login.py::test_login_success

我明白了:

================================================================ test session starts ================================================================
platform darwin -- Python 3.8.5, pytest-5.2.1, py-1.8.0, pluggy-0.13.0 -- /Users/mark/Python_Auto_Framework/com/companyname/venv/bin/python3
cachedir: .pytest_cache
rootdir: /Users/mark/Python_Auto_Framework, inifile: pytest.ini
plugins: testrail-e2e-2.0.0, xdist-1.30.0, bdd-3.2.1, forked-1.1.1, docker-compose-3.2.0, cov-2.8.1
collected 0 items                                                                                                                                   

=============================================================== no tests ran in 0.02s ===============================================================
ERROR: not found: 
/Users/mark/Python_Auto_Framework/com/companyname/Tests/test_login.py::test_login_success
(no name '/Users/mark/Python_Auto_Framework/com/companyname/Tests/test_login.py::test_login_success' in any of [<Module test_login.py>])

谁能告诉我我错过了什么?

更新: 我用 --debug 运行并查看了 pytestdebug 文件,我看到了以下内容:

matched test file 
'/Users/mark/Python_Auto_Framework/com/companyname/Tests/test_login.py' 
[assertion]
      found cached rewritten pyc for '/Users/mark/Python_Auto_Framework/com/companyname/Tests/test_login.py' [assertion]
      early skip of rewriting module: Pages [assertion]
      early skip of rewriting module: Pages.LoginPage [assertion]
      early skip of rewriting module: Core [assertion]
      early skip of rewriting module: Core.Utils [assertion]

但是在所有目录上递归查找编译后的副本什么都没有显示

【问题讨论】:

  • 如果直接引用测试,还必须添加测试类,例如test_login.py::TestLogin::test_login_success.
  • 错误:未找到:/Users/markdevine/IdeaProjects/Python_Auto_Framework/com/companyname/Tests/test_login.py::TestLogin::test_login_success(没有名称'/Users/mark/Python_Auto_Framework/com/ companyname/Tests/test_login.py::TestLogin::test_login_success' 在任何 [])
  • 尝试从项目的根路径运行python -m pytest(不确定是哪个)。
  • python -m pytest 也找不到测试
  • 您能否重试将您的测试目录从Test 重命名为test?并且,确保该目录包含__init__.py

标签: python python-3.x pytest


【解决方案1】:

尝试在根目录中添加 pytest.ini 文件并将目录名称(包含您的测试用例)添加到测试路径。下面是例子:

pytest.ini

[pytest]
testpaths = Tests

【讨论】:

  • 我在根目录中有 pytest.ini。我添加了带有测试路径(testpaths = com/companyname/Tests)和没有(testpaths = Tests)的测试路径。都没有用
  • 看起来您的测试用例在目录tests 下。在pytest.ini 文件中添加testpaths = tests。它应该工作
  • 当我根据 cmets 更新时,我总是更新 ini。没有快乐
猜你喜欢
  • 2019-06-20
  • 2019-06-16
  • 2023-01-27
  • 2019-10-16
  • 1970-01-01
  • 2015-07-22
  • 2020-12-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多