How to run pytest on Eclipse? - python

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

Related

How to make pytest cases "runnable" in IntelliJ

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.

How to run a particular test in PyCharm

I have 60 functional tests in one file. I wrote them in Notepad++ and used py.test as the test framework. Today I decided to swap Notepad++ with PyCharm. I opened my file of functional tests in PyCharm and ran the tests from PyCharm, as you can see in the picture:
Now, after confirming that I could run all of the tests, I tried to run an individual test, for example test_login_with_extantUser_using_email. Logically, I right-clicked on the test, expecting a "run test" button or something similar to appear. But no such thing appeared. In fact, it appears that there is no way to run an individual test by simply right-clicking on it.
So my question is, how can I run an individual test? Must I set up a configuration for each one in the Edit Configurations menu? That would take a very long time, considering that I have 60 tests.
I have found the problem. It's a bug in Pycharm that needs to be fixed. I will explain the bug.
Right-clicking an individual test will not display the option to run the test if the test is not a member of a class that inherits from unittest.TestCase. This is true even if you are not using unittest, as in my case, in which I am using py.test.
When I made my py.test test classes inherit from unittest.TestCase, I got the option to run tests individually when I right-clicked on a test.
I have reported the bug to Pycharm. Time will tell if they fix it.
https://youtrack.jetbrains.com/issue/PY-26754
Base on pt.test doc you can specify particular test_case for running with -k (which means keywords obviously). In your case it should be like pytest -k "test_login_with_extantUser_using_email" if you running in CLI.
If you want to run particular test with Pycharm, you have to fill 'Keywords' field in Run/Debug Configurations.
Best Regards.

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.

Can PyCharm drop into debug when py.test tests fail

When running tests with py.test there is a --pdb option to enter pdb on failure.
Is there a similar way to enter the debugger when running the same test from within PyCharm?
There is a py.test plugin, pytest-pycharm, that will halt the PyCharm debugger when a test emits an uncaught exception.
Follow the steps below to setup the correct run configuration:
Run > Edit Configurations...
Click the '+' button to add a new configuration.
Name the configuration and specify the configuration parameters below:
Script: The path to your py.test executable (find by typing which py.test)
Script Parameters: This parameter is your test case followed by the --pdb option (ie /Users/Johan/projects/misc/testing.py --pdb)
After setting up the configuration, you can run the test case from within PyCharm. If a test case fails, your PyCharm run window will show the pdb prompt.

Categories

Resources