Python Unit-Testing All Test Cases

I am running unit tests on pycharm but some how am discovering something weird. When I started testing a class, more specifically its methods, I simply wrote test cases like the following:

class hey:

    def hello(self):
        return True

    def bye(self):
        return 'Bye'

from unittest import TestCase
class TestHey(TestCase):
    def test_hello(self):

    def test_bye(self):

When I click run, it will automatically run all tests. Great. But since today, for some of my other custom classes, it seems to only run the first one. ie) just running test_hello and totally not testing test_bye. I would provide an example, but the behavior is not consistent as it works sometimes and it doesn't the next. I just wanted to inquire if anyone know if I am totally missing something?

EDIT: When I comment out the test cases that are actually being ran and leaving the ones that arent ran, i get the following error:

Traceback (most recent call last):
  File "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 3.4.1\helpers\pycharm\", line 148, in <module>
    testLoader.makeTest(getattr(testCaseClass, a[2]), testCaseClass))
AttributeError: 'TestLoader' object has no attribute 'makeTest'

Asked by: Haris416 | Posted: 06-12-2021

Answer 1

You can run tests from any number of places in PyCharm. Depending how you run the tests will change what tests are run.

If you right click the project or a folder then all tests within the project or folder (and its sub folders) will be run. Right click a file then all tests in the file will be run. Right click in a test case then only that test case is run. Right click inside a test method and then only that test method is run.

If you use the run configurations from the start button up top then the last known test configuration will be run. So if you were only running specific tests within that file then only those tests will be run again. You may have changed the run configuration from running all tests if you chose to re-run only failed tests at any point.

To solve the issue just right click the file or test case you want to run and select "Run 'Unittests in ...'". Would provide a screenshot, but PyCharm seems to be swallowing my keypresses.

Answered by: David559 | Posted: 07-01-2022

Answer 2

For me, I messed up the indentation of tests.

Answered by: Madaline821 | Posted: 07-01-2022

Similar questions

Python, unit-testing and mocking imports

I am in a project where we are starting refactoring some massive code base. One problem that immediately sprang up is that each file imports a lot of other files. How do I in an elegant way mock this in my unit test without having to alter the actual code so I can start to write unit-tests? As an example: The file with the functions I want to test, imports ten other files which is part of our software and not pytho...

Python unit-testing with nose: Making sequential tests

I am just learning how to do unit-testing. I'm on Python / nose / Wing IDE. (The project that I'm writing tests for is a simulations framework, and among other things it lets you run simulations both synchronously and asynchronously, and the results of the simulation should be the same in both.) The thing is, I want some of my tests to use simulat...

python - Unit-testing extensions for an 'external' program with pyunit

I'm struggling to know where to start with unittest, having read the dive-into-python tutorial and looked at I've got a piece of analysis software (call it 'prog.exe') which uses python for its input decks. I've started writing a python module which I'm going to import from that input deck to provide some useful functionali...

python - Tunnel interface unit-testing

I've made a Tun class that wraps pytun.TunTapDevice : from pytun import TunTapDevice class Tun(object): def __init__(self,name='tun',addr=None,dstaddr=None,netmask=None,mtu=None): tun = TunTapDevice(name=name) if addr : tun.addr = addr if dstaddr : tun.dstaddr = dstaddr if netmask : ...

python - Flask blueprint unit-testing

Is there a good practice to unit-test a flask blueprint? I didn't found something that helped me or that is simple enough. // Edit Here are my code: # -*- coding: utf-8 -*- import sys import os import unittest import flask sys.path = [os.path.abspath('')] + sys.path from ap...

python - Django unit-testing client.login not working

I am using django 1.6 and I have the following test code: def tes_stuff(self): new_user=EndUser.objects.create(username="test", firstname="test", email="", password="test") self.assertTrue(self.client.login(username="test", password="test")) When I run it I get an arror that says: AssertionError: False is not true I am unsure why it is not l...

Is there a preferred BDD style unit-testing framework for Python?

I was wondering if there are any BDD-style 'describe-it' unit-testing frameworks for Python that are maintained and production ready. I have found describe, but it doesn't seem to be maintained and has no documentation. I've also found sure which reached 1.0, but it seems to just add syntactic sugar i...

python - Unit-Testing Flask application views & dialogs

I am new to Flask and have recently started working on a project. After I've fixed some issues related to dialogs and views the client asked to write unittests to verify the resolutions. I've gone through some already written tests but they mostly just check the status code of http response, like this: def test_home_page(self): rv = self.client.get('/home') self.assertEqual(rv._status_code, 200, rv....

What's the right way of unit-testing a python class?

I'm not sure what's the best way to build independent unit tests on methods of a python class. Here is a trivial example: class MyClass(object): def __init__(self, data): = data def myMethod(self): #do something return True I'm building unit tests like this: class TestMyClass(unittest.TestCase): def test_init(self): mydata = mock.Mock() ...

django - Unit-testing Python: Mocking function calls inside function

I have a django view like this # Django view from some_module import f2 def f1(request, version): # some code f2(**kargs) # more code return HTTPResponse(response) The function f2 is in another module # some_module def f2(**kargs): # some code The Django view is part of an API so, the request and response are in json How do I...

Still can't find your answer? Check out these communities...

PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python