简体   繁体   English

pytest如何找到要测试的文件?

[英]How does pytest find files to test?

I know how py.test discovers tests to run, but I don't know how to make the tests refer to the files that contains the code to test. 我知道py.test如何发现要运行的测试,但是我不知道如何使测试引用包含要测试的代码的文件。

My project looks like this: 我的项目如下所示:

arith.py
tests/
    test_airth.py

In my project root directory, I have the file arith.py with 在我的项目根目录中,我有arith.py文件,其中

def plus(x, y):
    return x + y

In the file tests/test_arith.py I have 在文件tests/test_arith.py我有

import arith
def test_plus():
    assert arith.plus(3, 5) == 8

When I run, in the project root directory, 当我运行时,在项目根目录中,

pytest

It discovers my test file, because I followed the test discovery rules. 它会发现我的测试文件,因为我遵循了测试发现规则。 But pytest replies with 但是pytest回复

ImportError while importing test module tests/test_arith.py' 导入测试模块tests / test_arith.py时的ImportError

I ran pytest from the project root, so why does it not like my import? 我从项目根目录运行pytest ,为什么它不像我的导入一样? It wants to find the files that I am testing in the tests directory! 它想在tests目录中找到我正在测试的文件!

How can I set up the imports properly, so that my test files in the tests/ directory, can see the files under test which are in the project root directory? 如何正确设置导入,以便我在tests/目录中的tests/文件可以看到项目根目录中的被测试文件? (The examples in pytest's documentation seem to not do anything special...perhaps the tests are in the same directory as the files under test? They describe test discovery in great detail, but I must have missed how to set up the tests so they can see the code they are testing.) (pytest文档中的示例似乎并没有做任何特别的事情……也许测试与被测文件位于同一目录中?它们详细描述了测试发现,但我一定错过了如何设置测试的方法,因此它们可以看到他们正在测试的代码。)

Assuming you have a layout like this (which is what I do): 假设您有这样的布局(这就是我要做的):

.
├── myprogram
│   ├── __init__.py
│   ├── core.py
│   └── db.py
├── setup.py
└── tests
    ├── test_core.py
    └── test_db.py

You should be invoking pytest like so: 您应该像这样调用pytest:

py.test tests

I'm fairly sure that's an absolute/relative path to the tests directory. 我相当确定这是测试目录的绝对/相对路径。 This also assumes that you have done something like python -m pip install -e . 这还假设您已完成python -m pip install -e . in your root directory. 在您的根目录中。 And you have a .git or .hg folder there, too :) 而且您也有一个.git.hg文件夹:)

It's a little strange so much is needed. 这有点奇怪,所以需要很多。 Surely there is a better way? 当然有更好的方法吗?

Actually that is the best way. 其实那最好的方法。 I don't think it's controversial to declare that the best way to distribute your Python application is as a Python package, preferably a wheel . 我认为宣布Python应用程序的最佳分发方式是Python软件包(最好是wheel)并没有争议。 To do that, you're probably going to need a setup.py . 为此,您可能需要setup.py

By installing your package (preferably in a venv/virtualenv sandbox) you're ensuring that you know what packages are required to install/run yours . 通过安装你的包(最好在VENV / virtualenv中沙箱)你确保你知道软件包安装/运行需要什么。 If your package is installed that means that any script that does import myprogram (using my example layout) will be able to import your package. 如果安装了软件包,则意味着任何import myprogram脚本(使用我的示例布局)都将能够导入您的软件包。 Which is what you'll be doing in your test suite. 这就是您在测试套件中要做的事情。

Then your test discovery is as simple as telling pytest where to find it. 然后,您的测试发现就像告诉pytest在哪里找到一样简单。

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

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