How to make pytest cases "runnable" in IntelliJ - python

I want to write my test using pytest and to be able to run them (individually) in IntelliJ. I have pytest installed, along with (obviously) Python plugin for the IDE.
My test file (tests/test_main.py) looks like that:
from app.main import sum_numbers
def test_sum_numbers():
assert sum_numbers(1, 2) == 3
assert sum_numbers(10, 20) == 30
assert sum_numbers(1, 2, -3) == 0
Running pytest from terminal works perfectly. But IntelliJ seems to treat this as a regular file, there are no "run" icons before the declarations. Also, running the file from the IDE does nothing, as it is not treated as a test file.
What can I do?

Here what you need to do:
1) Remove all the existing run configurations for your test file.
2) Make sure that Preferences | Tools | Python Integrated Tools | Default rest runner is set to pytest.
After that, you should see the run icon next to your test functions and right-clicking the test file should suggest running the pytest.
See the docs for more details.

Open the source code, right-click and hit run using the option run "filename.py".
IntelliJ will make the configuration for you and then you can click the run button after the configuration is made by the IDE.
Same thing for debug configuration.
Also, the IDE gives you the run button on individual tests after this.

Related

How to run unittest in pycharm community v2018.3.6

I am trying to run unit test in pycharm but not seeing an option in the context menu..
Please find as show below
Please help
Also, when I run from the following command from the console, 'python -m unittest login.py' unit test are running but again if I run command 'python login.py' actual test execution is not running.
How to run unit test and test execution from console or from pycharm IDE ..??
Looks like you have an existed run configuration (login) for this script so PyCharm suggests it instead of running tests. Either remove it from Run | Edit configurations and try to right-click -> Run unittest or use Run | Run... and select unittest there. Has it helped?
Thank you Pavel for respond, I have solved by renaming file name starts with 'test', it worked now, not sure if this is really the way to fix but it resolve my problem.

Run single test using Python Unittest in Visual Studio Code Debug mode

I use the pylint extension in Visual Studio Code within the Debug module (see image) to run my tests using Python Unittest library.
Whenever I run my tests, I execute my test.py file in Debug and it runs all of the tests in the entire file. I have my tests broken up logically by classes with several tests per class.
To reduce the time it takes evaluate test results for a single test I am actively working on, is there a way to execute just that one test rather than wait for all the tests in the test.py file to execute while in VS Code Debug mode?
For example:
test.py
import unittest
class test_TestClass(unittest.TestCase):
def test_Test1(self):
x = 1
self.assertEqual(x, 1)
def test_Test2(self):
y = 2
self.assertEqual(y, 3)
If I want to only execute test_Test2() to make sure that it fails, how do I do that without running the entire file (i.e., test_Test1 and test_Test2)?
When your tests are discovered by Python: Run All Unit Tests (or clicking on the Run Tests button in the status bar and choosing Discover Unit Tests), a code lens is provided to run individual test functions, methods, and classes either normally or under the debugger.
Clicking on Debug Test will run open the debugger and run the unit test under it. Otherwise you can modify your launch.json to add a debug configuration and specify the arguments to your test runner to just run the test(s) you're interested in.

How to run pytest on Eclipse?

I'm new to unit testing and "im trying to run a pytest on Eclipse. I have searched for hours now and I cant seem to find out what the problem is. Im playing around with the simple examples from the https://pytest.org website. My problem is that pytest just does not run on Eclipse. I can use the command prompt to do the tests, but I would much rather have the results on the console window.
Things I have tried but didnt work;
setting PyUnit test runner to Py.test runner (instead of the default Pydev test runner)
In this case I get the following error message
usage: runfiles.py [options] [file_or_dir] [file_or_dir] [...]
runfiles.py: error: unrecognized arguments: --verbosity inifile:
None rootdir: C:\peepee\pytest\testing
I have set the verbosity to 9 (read somewhere that its the maximum). Didnt make any difference.
Simple code I'm trying to test from the http://pytest.org website
def func(x):
return x + 1
def test_answer():
assert func(3) == 5
Works through the cmd but not on Eclipse.
Please help, as I'm losing time on trying to figure this out. Thanks in advance
It didn't work for you because you left "--verbosity 0" in the parameters text field. I don't know why it's not automatically erased by Eclipse, but when you change the runner you MUST also change the parameters to reflect your preferred test runner (pytest in this case).
Globally for all new configurations:
Window -> Preferences -> PyDev -> PyUnit
Change the test runner to "py.test runner" and clear the parameters (or add the ones you prefer. Make sure they are valid flags for pytest.)
Or, if you prefer, manually for each new run configuration
Create a Python unittest run configuration
Select the class you want to run and the project (if they are not already there)
In the Arguments tab override the pyUnit preferences with a py.test runner (clean up the parameters and add whatever you want to add to pytest flags)
1) open run configurations from run menu
2) right click on python unittest and select new to configure a new configuration
3) select the project and the in the main module, select the module which has your test cases.
4) under arguments tab, check 'override pyunit preferences for this launch'.
5) select Py.test runner from the drop down.
6) type --tb=short and --capture=no (values can be changed depending on user preference)
7) click apply and then click run.
NOTE: If you are using django-configurations for your settings then you MUST set the DJANGO_CONFIGURATION environment variable in the environment variables section of the debugger configuration to whatever you use for your testing runs from the command line.
Found an alternative. Just use the PyCharm IDE which makes pytest very easy to run. Make sure to do the following configuration before running any test.
Click the "Run" tab. Select "Edit configurations"
Add configuration with the "+" symbol
Select "Python test" under that "py.test"
Make sure to fill out the "Target" path and the "working directory"
Happy days. Now you have pytest running with the results displayed on the console window

Idea run/debug py.test single test not the whole suite

I'm creating a python test suite (using py.test). I'm coding the tests in Idea and I don't know how to debug a single test.
This is my setting of the debugger. It runs the whole testsuite. So I have to run all the tests before it gets to the one I'm trying to debug.
In your configuration, set:
Target to the relative path of one of your test files, i.e. testsuite/psa/test_psa_integration.py
Keywords to a keyword that identifies the test you are trying to run specifically. If tests are part of a class, Keywords should be something like: TestPsaIntegration and test_psa_integration_example
I don't use IntelliJ, but in PyCharm, you can easily debug tests without going through this tedious process of adding a Run/Debug configuration each time.
To do this with PyCharm, go to:
Preferences (or Settings) > Tools > Python Integrated Tools and set Default test runner to py.test.
Then, back in your file (i.e. test_psa_integration.py), you could just right-click anywhere within the code of a test, and select either Run 'py.test in ...' or Debug 'py.test in...' which will automatically create a new Run/Debug configuration as explained previously.
An alternative is adding --no-cov --capture=no into Additional Arguments. To make this automatic for other test file, add those into the Template part.

How do I run doctests with PyCharm?

In the PyCharm IDE, if I right-click on a function/method with a doctest, sometimes the right-click menu will give me the option: "Run 'Doctest my_function_name'" and sometimes the right-click menu, instead, only gives the option to run the whole file (NOT as a doctest).
What determines when it will give the "run doctest" option and when it will not? Is there a way to force it one way or the other?
Running a module (or the tests in it) in PyCharm is done via a Run Configuration. When you right click a module, PyCharm searches for an existing Run Configuration for that module. If a configuration is found (this can be due to a previous run, or a manual creation of a Configuration), PyCharm will only suggest to run that configuration.
For example, if a configuration of module.py was created to run its doctests, that is the option we'll see when right-clicking module.py. However, if no configuration is found, PyCharm suggests to run the module in different options, depending on the code in the module (run regularly, or run doctests / unittests). Once an option is chosen, PyCharm creates the respective, temporary, Run Configuration, implicitly. From here on, when right clicking the module, you'll only get the configuration that was created for that module.
Important side note: PyCharm saves up to 6 temporary (i.e., Configurations that were created via running a module) Run Configurations- 3 in "Python", i.e., scripts, and 3 in "Python Tests". This means that if you run moduleA.py, moduleB.py, moduleC.py, and then moduleD.py, the temporary Configurations in PyCharm will be moduleB.py, moduleC.py, and moduleD.py. The configuration of moduleA.py will be automatically deleted, unless explicitly saved.
This behaviour can be reproduced as following:
In PyCharm, create a new Python module: "temp"
2.Add the following to the module:
"""
>>> print 3.14
3.14
"""
if __name__ == '__main__':
pass
Right click on the doctest section gives the option to "Run 'Doctests in temp' "
Right click on the main section gives the option to "Run 'temp' "
Choosing anyone of the options, makes the other option disappear in subsequent runs. E.g., choosing to run the module will make the option to run Doctests disappear in subsequent runs, and vice versa.
Going back to the first stage, where it was possible to choose between the two options, is possible by deleting the module's "Run configuration":
Run --> Edit configuration --> Locate the module's current configuration (usually highlighted) --> Click the "Minus" button (top left corner) --> Click "Apply" --> Click OK.
Now we are back at step 3.
(Reproduced in PyCharm 5.0 and 4.5)
To summarize:
If no Run Configuration is found, PyCharm suggests to run the module in any possible way (as a script, doctests, or unittests)
If a Run Configuration is found, PyCharm only suggests that Configuration.
If PyCharm isn't giving you the run option that you want, find the Run Configuration that is preventing it from giving you that option and delete it, or create a new one that will run the file, or method/function, the way you want.
If you don't want to delete configurations, you can also hit the shortcut key for Run | Resume Program (F9 for me) to pop up a complete list of choices
If the above doesn't work for you - make sure that your module is not named doctest; it will cause a conflict and therefore cause the exception.

Categories

Resources