【发布时间】: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