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

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.

Related

How can I modify the value of "too-many-statements" from 50 to 100 in the pylintrc file?

When running Pylint on my file I am getting the following message.
refactor (R0915, too-many-statements, function) Too many statements (95/50)
I want to set the number of statements a function can have to 100 instead of 50 in order avoid the above message from Pylint.
Pylint works based on configured settings which are default PEP 8 standards. Now, if customizing them is good or bad can be taken for another discussion, as they are kept like that for a reason. For example, if you have a method with more than 50 lines of code, it simply means that you are increasing the cyclomatic-cognitive complexities as well as making it difficult to unit test and get coverage.
OK, arguments aside, I think the following approach can help you customize the linting rule.
Go to your Python site-packages directory (it might be inside the Python installation Libs folder or in your virtual environment.
For example, D:\Python37\Lib\site-packages
Open a command line here, and navigate to the Pylint directory. Execute the configuration generator like
lint.py --generate-rcfile > custom_standard.rc
Now you will have a file named custom_standard.rc in the folder. Let’s copy it to some place around your project, say, D:\lint_config\custom_standard.rc.
Open the configuration file. You can see a setting for most of the rules. Now, for your question of number of statements inside a method, find the setting called
max-statements=50
Change it to:
max-statements=100
Save the configuration file. Now when you run the Pylint executable, use the option --rcfile to specify your custom configuration:
pylint --rcfile=D:\lint_config\custom_standard.rc prject_dir
If you want to integrate this with your IDE like PyCharm there are plugins which allows configuring the same.
But again!, it is not a good decision to change the PEP 8 :-)

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.

Restart the process where the error has been detected

In a Django project, it is possible to create unit-tests to verify what we had done so far. The principle is simple. We have to execute the command python3 manage.py test in the shell. When an error is detected in the program, the shell will display it and stop the process. However, the procedure has a little gap. If we have several errors, we have to correct it and restart the whole process. This process could take several minutes which depends of our program. Is there a manner to restart the process where the error has been detected instead of restart the whole procedure?
EDIT :
In fact, another problem I have is to retain the databases instead of recreate it. How could I do such thing?
If you want to automatically run only failing tests you need to use a third party testing driver like Nose or create your own. But it's not worth it because ...
You can specify particular tests to run by supplying any number of
“test labels” to ./manage.py test. Each test label can be a full
Python dotted path to a package, module, TestCase subclass, or test
method. For instance:
# Run just one test method
$ ./manage.py test animals.tests.AnimalTestCase.test_animals_can_speak
Source: https://docs.djangoproject.com/en/1.10/topics/testing/overview/
This approach can be used to re run only the ones that have failed.
Please note that third party test runners will probably recreate the database every time you run the test - even for only the failing test. On the other hand the django default test runner has the --keep option which allows the database to be reused. For more details see: https://stackoverflow.com/a/37100979/267540

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

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