PyCharm - no tests were found?
Asked Answered
S

13

52

I've been getting na error in PyCharm and I can't figure out why I'm getting it:

No tests were found

This is what I have for my 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:

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 this SO post but I'm still utterly confused. My run configuration currently looks like this:

screenshot of run configuration

Sufficient answered 12/3, 2016 at 23:59 Comment(1)
perhaps this will be useful to you: intellij-support.jetbrains.com/hc/en-us/community/posts/… to stop running in test mode.Gudrunguelderrose
A
75

In order to recognize test functions, they must be named test_. In your case, rename xyCheck to test_xyCheck :)

Adar answered 13/3, 2016 at 0:5 Comment(2)
Echoing what Ilya's answer, you also need to name the test files test_... otherwise you can't run the tests from their directoriesMidgut
Keep in mind, if your tests are inside of a class, the class name should also start with the word Text. This question uses the class name TestPoint, which is correct. You could have the same error if your class name was PointTest.Strife
J
47

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.

  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

I'm using Intellij IDEA 2016.3.5 with Python plugin

If you want to run you tests with command line

python -m unittest

Then you should add __init__.py to 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.

Ja answered 13/4, 2017 at 9:26 Comment(2)
For me the file containing the tests has to start with test_Barnyard
File containing the test should also start with "test". "Test" (with capital T doesn't work). Saved me!Postulant
M
16

One thing that can also cause this problem is if you have not selected the right testing framework in your settings:

settings > Python Integrated Tools > Testing > Default test runner

All my tests are in pytest, but it was set to unit test

Morrissey answered 28/9, 2020 at 12:49 Comment(0)
E
7

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.

Extrusion answered 21/11, 2017 at 9:17 Comment(0)
L
6

Another gotcha that has just bitten me.

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

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

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.

Lolanthe answered 28/3, 2018 at 13:54 Comment(0)
H
4

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 Keyword to the Run/Debug Configuration, and Pycharm will only run tests whose name contains the keyword.

Hermaphroditus answered 17/8, 2016 at 22:6 Comment(0)
S
4

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.

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.

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

enter image description here

Steed answered 9/10, 2017 at 22:0 Comment(0)
A
3

None of these helped me, but I was able to fix the issue by setting the working directory to the project directory.

I debugged this by first creating a new Django test configuration witj a target of one of the app names with tests.

That ran successfully. No idea why working directory should need to be specified.

Aqueduct answered 1/7, 2022 at 7:35 Comment(0)
P
2

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.

The exception was happening in unittest's loader.py

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

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

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:

from .model_factories import UserFactory, UserGroupFactory, ApplicationFactory

Which worked!

Posterity answered 21/6, 2018 at 17:45 Comment(0)
S
1

One way i got away from this error is i have deleted the extra configurations which i have added to run the Pytest file. Hope it would work for you too.

Samarium answered 6/3 at 4:24 Comment(0)
N
0

i found the solution , im a beginner , forgive me if its not working,

  1. if unittest in pycharm is not working ...(you will know when , even your file to test is deliberately wrong , pycharm will tell you its OK , or pass)
  2. pres CRTL ALT S ---->> Testing ---->> change to pytest , will show hazard like symbol at bottom ---->> press fix ---->> apply ---->> ok..
  3. the only modification to your code will be to add this in the end . if name == 'main': unittest.main() (double underscore name double underscore == ' double underscore main double underscore ' :
  4. hope it works ...
Noemi answered 18/3, 2023 at 8:48 Comment(0)
S
0

In my case one of parent folders of the test did not begin with test_.

Steger answered 20/5, 2023 at 6:48 Comment(0)
C
0

For my case, the steps as below

00 use Python Integrated Tool set Default test runner to pytest

enter image description here

01 delete all this: enter image description here from enter image description here

Candlewood answered 4/3 at 10:14 Comment(1)
Welcome to StackOverflow. Usually, a short description of the linked pictures are helpful to others.Colobus

© 2022 - 2024 — McMap. All rights reserved.