Python 在子文件夹中使用pytest where test
我正在使用PythonPyTest来运行我的单元测试。 我的项目文件夹是:Python 在子文件夹中使用pytest where test,python,unit-testing,pytest,Python,Unit Testing,Pytest,我正在使用PythonPyTest来运行我的单元测试。 我的项目文件夹是: Main-包含数据文件:A.txt Main\Tests-运行pytest的文件夹 Main\Tests\A\u test-包含测试文件的文件夹 _test文件夹中的测试使用文件A.txt(位于主文件夹中) 我的问题是,当我运行py.test时,测试失败,因为它找不到A.txt 我发现这是因为pytest在运行测试时使用路径Main\Test,而不是将路径更改为Main\Tests\A\u Test(在测试文件中打开A.
Main
-包含数据文件:A.txt
Main\Tests
-运行pytest的文件夹
Main\Tests\A\u test
-包含测试文件的文件夹
_test文件夹中的测试使用文件A.txt
(位于主文件夹中)
我的问题是,当我运行py.test时,测试失败,因为它找不到A.txt
我发现这是因为pytest在运行测试时使用路径Main\Test
,而不是将路径更改为Main\Tests\A\u Test
(在测试文件中打开A.txt
时使用相对路径)
我的问题:有没有办法让pytest将目录更改为它为每个测试执行的测试的文件夹?所以测试中的相对路径仍然有效
有没有其他通用的方法来解决这个问题?(我不想将所有内容都更改为绝对路径或类似的内容,这也是一个示例,在现实生活中,我有数百个测试)
谢谢,
诺姆我算是解决了这个问题,不确定这是最好的方法,但它正在发挥作用: 在每项测试中:
\Main\Tests
\Main\Tests
执行的,则Ichdir
到\Main\Tests\A\u test
defsetupclass
方法下执行此操作
例如:
@classmethod
def setUpClass(cls):
if (os.path.exists(os.path.join(os.curdir, "A_test"))):
os.chdir("A_test")
无论是从Tests
文件夹(使用pytest)还是从A\u test
文件夹(通过pycharm)选项A-最小解决方案执行,这都会使测试通过
在项目的根目录下,创建一个名为tests.py的文件,其中包含以下内容
import os, pathlib
import pytest
os.chdir( pathlib.Path.cwd() / 'Tests' )
pytest.main()
然后可以使用命令python tests.py
运行测试
选项B-使用批处理/bash测试运行程序 对于那些喜欢使用batch/bash来运行脚本的人,我们可以更改batch/bash中的目录,然后调用运行pytest框架的Python脚本。为此,请在项目文件夹中创建以下脚本 测试.bat(适用于Windows) test.sh(适用于Linux) 然后在Tests文件夹中,创建一个名为runner.py的文件,其中包含以下内容
import pathlib, sys
import pytest
cwd = pathlib.Path.cwd()
# Add the project's root directory to the system path
sys.path.append(str( cwd.parent ))
# This is optional, but you can add a lib directory
# To the system path for tests to be able to use
sys.path.append(str( cwd / 'lib' ))
pytest.main()
如果您的目录结构在Tests文件夹中包含某种类型的lib文件夹,我们可以通过创建一个pytest.iniconfig文件来指示pytest忽略它
[pytest]
norecursedirs = lib
在这种情况下,您的目录/文件结构最终将是:
root
├── test.bat
├── test.sh
├── Main
└── Tests
├── runner.py
├── pytest.ini # Optional pytest config file
├── lib # Optional, contains helper modules for the tests
├── tests # Tests go here
└── # Or, in the OPs case, you could also place all of your tests here
补充意见 上面的方法不是运行pytest的典型方式,但我更喜欢使用
pytest.main()
,因为它允许我们:
- 有任何目录结构
- 在测试运行程序启动之前执行代码
- 您仍然可以传入命令行选项,它的行为将与直接运行
命令完全相同pytest
sys.path中的根Main
给出当前目录的是Main/
:
$python -m pytest Tests/A_test
这将把Main
附加到sys.path
中,并在A_test
子目录中运行测试。
关于pythonpath和pytest关系的更多信息,请参见此处:将\uuuu init\uuuuu.py
添加到对我有用的测试包中。所有测试都是在之后执行的。我认为“最真实”的答案(可能取决于意见)来自测试本身:可以在setup.cfg
,pytest.ini
,tox.ini
中设置测试路径
配置选项,或者pyroject.toml
文件。既然你提出了解决方案,我不想让你觉得做一个新的答案是正确的。但是您也可以将setUpClass
方法中的代码添加到A_test/\uuuu init\uuuu.py
文件中。这样,如果目录中有多个测试,则不需要将代码添加到每个文件中。这是PyCharm Friendly这个解决方案的问题是我不确定如何利用PyCharm作为我的测试运行程序。我发现经过批准的解决方案具有较少的升级投票数,这对PyCharm更有利。在test dir中添加\uuuu init\uuuu.py
会使pytest将它们添加到pythonpath中,但这意味着如果您有一个名为“os”的目录,它将与python包os发生冲突,并造成严重破坏。这种情况经常发生,我不推荐。
root
├── test.bat
├── test.sh
├── Main
└── Tests
├── runner.py
├── pytest.ini # Optional pytest config file
├── lib # Optional, contains helper modules for the tests
├── tests # Tests go here
└── # Or, in the OPs case, you could also place all of your tests here
$python -m pytest Tests/A_test