The decorator @unittest.SkipTest prevents a unittest from being executed automatically when running the unit tests in a test class. Unfortunately, it also makes
the individual execution in PyCharm (by right-clicking on the function and
selecting Run/Debug for this test method) fail with a TypeError: don't know how to make test from: <function .... at 0x7fbc8e1234c0>
Is there a way to disable a unit test from automatic (bulk) execution
when running the complete test class, but leaving it executable by hand (preferably in PyCharm) such that it can be run without the need to make any changes in the test file?
I thought
@unittest.skipUnless(condition, reason)
might possibly come in handy, but could not come up with a condition that is satisfied only when the test is
launched by hand. Any suggestions appreciated!
2 Answers
Have you tried including a parameter that is set when run through CI/CD? You could have the default value set to false, and then CI/CD sets it to true. This would give you the condition you are looking for.
As far as I'm aware, the only way to differentiate between CI/CD runs and IDE runs is through some intermediary parameter that you must set.
Edit: Try setting a custom build configuration in PyCharm for that specific test. You could have that build configuration pass in a parameter to your testsuite. At that point you would have a proper condition to have this test not be skipped when you run tests using the command line vs PyCharm's integrated test runner.
For simplicity, you'll want the default value of the parameter to be to skip, and only set the Boolean value to not skip by passing in True to that param in the special build config in PyCharm.
See: https://www.jetbrains.com/help/idea/build-configuration.html
5 Comments
I would try to control this with a parameter that is set only in PyCharm or another IDE.
That is, you could use skipUnless(...) with a condition relating to an environment variable defined in your PyCharm test configuration.
Here is a complete example:
import os
import unittest
def is_prime(n):
""" Return whether `n` is prime. """
return n == 2 or not (n % 2 == 0 or any(n % i == 0 for i in range(3, n, 2)))
class IsPrimeTest(unittest.TestCase):
@unittest.skipUnless(os.getenv('MYPROJECT_DEVELOPMENT_TEST'), reason="Lengthy test")
def test_is_prime(self):
self.assertTrue(is_prime(2))
self.assertTrue(is_prime(3))
self.assertFalse(is_prime(4))
self.assertTrue(is_prime(5))
When run from the shell, the tests are indeed skipped:
$ python3 -m unittest test_example.py
s
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK (skipped=1)
If you select "Edit Configurations...", you can set the specific environment variable in the test configuration, as shown in the screenshot below. With this change, the test in question is actually run in PyCharm (not shown).
Addendum from my comment below:
One helpful option may be to create multiple test "Targets" in PyCharm, using the "+" button (the upper left corner of my screenshot). Then you could have one target that includes the test, and one target that does not, naming them "Standard unit tests" and "All unit tests", for example. To switch between them, you could choose each in a certain dropdown next to a green "play" button (toolbar in my main IDE window). This seems pretty ergonomic and simple.

python -m unittest, or some other way?python -m unittest.