2

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
  • How are you “automatically” executing the tests? With py.test, or with python -m unittest, or some other way? Commented Nov 28, 2019 at 22:29
  • The latter, i.e. python -m unittest . Commented Nov 29, 2019 at 7:26

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your answer. I am not sure what you mean by "run through CI/CD"? I want the condition for execution to be false by default and just be able to manually trigger it when needed.
I think I misunderstood the question. To be honest, the cheapest way to get this done is to just comment out the annotation when you want to run the test by itself. Spending effort into a solution will likely not be worth the effort on your part (if a solution were to exist)
I revised my original answer. Maybe this is closer to something you're looking for
Thanks for your hints! I guess you are right that spending too much effort on this is not really worth it. Your suggestion to look into PyCharm build configurations is useful in any case, so +1 from me.
You're welcome. Don't forget, if you're done with the question select this as your chosen answer to close it off
1

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).

PyCharm unit test configuration showing environment variables set

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.

2 Comments

Many thanks for your detailed reply! As one still has to set the environment variable back and forth before execution, I guess commenting out the skip- decorator is almost easier. Unfortunately my upvote of your answer somehow disappeared and I cannot upvote it again - very sorry about that!
@ctenar, one 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.