【Pytest笔记】pytest-pythonpath插件的使用

来源:互联网 发布:福建祥游网络 编辑:程序博客网 时间:2024/05/17 03:40

pytest-pythonpath

This is a py.test plugin for adding to the PYTHONPATH from the pytests.ini file before tests run.


这句话的意思是说,我们可以测试中所需要加入到PYTHONPATH中的路径,写到pytest.ini中。这样再运行测试之前,pytest.ini中的路径会自动添加到PYTHONPATH变量里面。

Installation

Install with pip::

pip install pytest-pythonpath

Uninstall with pip::

pip uninstall pytest-pythonpath

Usage

Add a line in your pytest.ini file with a key of python_paths and provide a space seperated list of pathsyou want inserted to the beginning of the PYTHONPATH before any tests run:

[pytest]python_paths = your/path/apps your/path/libs
Once this is added to your pytest.ini, you can use the py.test command as you normally would.


上面是举了一个栗子,说如果想把your/path/apps和your/path/libs加入到PYTHONPATH中,需要把它们用空格隔开赋值给pytest.ini里面的python_paths。这样在测试执行之前,就会把your/path/apps和your/path/libs加入到PYTHONPATH中了。我们的测试中需要导入your/path/apps和your/path/libs中的模块时,就不至于发生improtError了。

验证

Github上有源码https://github.com/bigsassy/pytest-pythonpath

我们把源码下载下来,用py.test执行test_pythonpath.py,将运行正常。

但是,把源码中的pytest.ini给个其他名字,用py.test执行test_pythonpath.py.会发生improtError错误。这是因为我们没有把pytest.ini中的test_path/apps 和test_path/libs 导入到PYTHONPATH中。所以我们的test_pythonpath.py文件中将提示找不到所要导入的模块。说明,pytest.ini文件生效了。

0 0
原创粉丝点击