Python Pycharm - 没有找到测试?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35964848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:12:48  来源:igfitidea点击:

Pycharm - no tests were found?

pythonunit-testingpycharmnose

提问by ocean800

I've been getting a

我得到了一个

No tests were found

没有找到测试

error in Pycharm and I can't figure out why I'm getting it... this is what I have for my point_test.py:

Pycharm 中的错误,我无法弄清楚为什么我会得到它......这就是我的point_test.py

import unittest
import sys
import os

sys.path.insert(0, os.path.abspath('..'))

from ..point import Point


class TestPoint(unittest.TestCase):
    def setUp(self):
        pass

    def xyCheck(self,x,y):
        point = Point(x,y)
        self.assertEqual(x,point.x)
        self.assertEqual(y,point.y)

and this point.py, what I'm trying to test:

point.py就是我要测试的内容:

import unittest

from .utils import check_coincident, shift_point

class Point(object):
    def __init__(self,x,y,mark={}):
        self.x = x
        self.y = y
        self.mark = mark

    def patched_coincident(self,point2):
        point1 = (self.x,self.y)
        return check_coincident(point1,point2)

    def patched_shift(self,x_shift,y_shift):
        point = (self.x,self.y)
        self.x,self,y = shift_point(point,x_shift,y_shift)

Is it something wrong with my run configuration? I looked at thisSO post but I'm still utterly confused. My run configuration currently looks like this:

我的运行配置有问题吗?我看了这篇SO帖子,但我仍然完全困惑。我的运行配置目前如下所示:

enter image description here

在此处输入图片说明

I guess I just understand what I could be doing wrong? Any help would be greatly appreciated, thanks!!

我想我只是明白我可能做错了什么?任何帮助将不胜感激,谢谢!!

回答by Joran Beasley

in order to recognize test functions they must be named test_in your case rename xyCheckto test_xyCheck:)

为了识别测试函数,它们必须test_在您的案例中重命名xyChecktest_xyCheck:)

回答by Ilya Sazonov

I know it's more than a year since the question was asked, but I had the same issue and that post was the first result in search.

我知道这个问题已经一年多了,但我遇到了同样的问题,那个帖子是搜索中的第一个结果。

As I understood PyCharm (or Intellij Idea Python plugin) needs your test to meet the following criteria if you want it to be launched when you run all the tests in directory.

据我了解,如果您希望在目录中运行所有测试时启动 PyCharm(或 Intellij Idea Python 插件),则需要您的测试满足以下条件。

  1. test functions should start with "test" (underscore is not necessary)
  2. the file, containing the test should also start with "test". "Test" (with capital T doesn't work in my case
  1. 测试函数应该以“test”开头(不需要下划线)
  2. 包含测试的文件也应该以“test”开头。“测试”(大写 T 在我的情况下不起作用

I'm using Intellij IDEA 2016.3.5 with Python plugin

我正在使用带有 Python 插件的 Intellij IDEA 2016.3.5

If you want to run you tests with command line

如果你想用命令行运行你的测试

python -m unittest

Then you should add __init__.pyto test directory. Python still wants your test function names to start with "test", and you test file name to start with "test", but in case of files it doesn't care if the first "t" is capital or not. TestCase and test_case is equally fine.

然后你应该添加__init__.py到测试目录。Python 仍然希望您的测试函数名称以“test”开头,并且您的测试文件名以“test”开头,但对于文件,它并不关心第一个“t”是否大写。TestCase 和 test_case 一样好。

回答by Graeme

Another gotcha that has just bitten me.

另一个刚刚咬我的陷阱。

I had a test file within my test package called test_queue.pywhich followed all of the above advice, however when I selected "Run UnitTests" in PyCharm the console reported no tests found.

我的测试包中有一个测试文件,test_queue.py它遵循上述所有建议,但是当我在 PyCharm 中选择“运行 UnitTests”时,控制台报告没有找到测试。

The issue in my case was that I had a non-unit test file in the root of the project also called test_queue.pywhich had been used for some other purpose early on in the project and forgotten about.

就我而言,问题是我在项目的根目录中有一个非单元测试文件,也称为test_queue.py该文件,该文件在项目早期用于其他目的而被遗忘。

Even though I was specifically selecting the test file in my tests folder, and the path was set to absolutely point at the unit test version of the file, it seems the file in the root of the project was being used.

尽管我在我的测试文件夹中专门选择了测试文件,并且路径设置为绝对指向文件的单元测试版本,但似乎正在使用项目根目录中的文件。

So, one more thing to check, make sure there are no other files in your project with the same name.

因此,还有一件事要检查,请确保您的项目中没有其他同名文件。

回答by invalidusername

Don't use dashes ("-") in your filename. These files were being ignored on my machine. renaming them from project-tests.py to project_tests.py solved the problem.

不要在文件名中使用破折号(“-”)。这些文件在我的机器上被忽略了。将它们从 project-tests.py 重命名为 project_tests.py 解决了这个问题。

回答by Keith

Another issue you may want to check is if you see in the console output "## tests deselected by '-k XXXX'". This means you've added a Keywordto the Run/Debug Configuration, and Pycharm will only run tests whose name contains the keyword.

您可能要检查的另一个问题是,您是否在控制台输出中看到“ ## tests deselected by '-k XXXX'”。这意味着您已经在运行/调试配置中添加了一个关键字,Pycharm 将只运行名称包含该关键字的测试。

回答by Ben

Adding another answer in the hopes that it helps someone down the road. I've been fighting this problem and just figured out the answer (I think). I originally deleted the standard Django file tests.py out of my application folder. I also created a subdirectory of my project called tests, which contains separate test scripts. In this situation, Pycharm failed to find the tests. I corrected this simply by creating an empty file called tests.py in my application folder.

添加另一个答案,希望它可以帮助某人。我一直在与这个问题作斗争,只是想出了答案(我认为)。我最初从我的应用程序文件夹中删除了标准的 Django 文件 tests.py。我还为我的项目创建了一个名为 tests 的子目录,其中包含单独的测试脚本。在这种情况下,Pycharm 无法找到测试。我只是通过在我的应用程序文件夹中创建一个名为 tests.py 的空文件来纠正这个问题。

So:

所以:

  1. Make sure you have a file called tests.py in your application director (it can be an empty file)
  2. It seems that a folder called tests, in your project, can contain separate test scripts and Pycharm seems to find and run these.
  1. 确保您的应用程序目录中有一个名为 tests.py 的文件(它可以是一个空文件)
  2. 在您的项目中,一个名为 tests 的文件夹似乎可以包含单独的测试脚本,而 Pycharm 似乎可以找到并运行这些脚本。

Here's a picture of the directory structure that's working for me:

这是对我有用的目录结构的图片:

enter image description here

在此处输入图片说明

回答by James Bradbury

I had this exception when running individual tests in a Django 1.8 project in PyCharm 2018.1. I could run all the tests together, but individual tests in one file crashed.

在 PyCharm 2018.1 的 Django 1.8 项目中运行单个测试时,我遇到了这个异常。我可以一起运行所有测试,但是一个文件中的单个测试崩溃了。

The exception was happening in unittest's loader.py

异常发生在 unittest 的 loader.py 中

It was getting an ImportError trying to import test_admin_views.py, though the exception was hiding the details of that error.

尝试导入 test_admin_views.py 时出现 ImportError,但异常隐藏了该错误的详细信息。

To see the details of the ImportError, I opened a Python Console and ran:

要查看 ImportError 的详细信息,我打开了一个 Python 控制台并运行:

import my_app.users.tests.test_admin_views

This gave me:

这给了我:

Traceback (most recent call last):
  [...]
  File "my_app/my_app/users/tests/model_factories.py", line 42, in <module>
    from my_app.applications.tests.model_factories import ApplicationFactory
ImportError: cannot import name ApplicationFactory

I noticed that some of the other factories in that file are imported without using the full path, so I tried adding ApplicationFactory to the relative path import list:

我注意到该文件中的其他一些工厂是在不使用完整路径的情况下导入的,因此我尝试将 ApplicationFactory 添加到相对路径导入列表中:

from .model_factories import UserFactory, UserGroupFactory, ApplicationFactory

Which worked!

哪个有效!