Run button to test function disappeared in pycharm [duplicate] - python

I want to start writing unit tests for my Python code, and the py.test framework sounds like a better bet than Python's bundled unittest. So I added a "tests" directory to my project, and added test_sample.py to it. Now I want to configure PyCharm to run all the tests in my "tests" directory.
PyCharm allegedly supports py.test in its test runner. You're supposed to be able to create a run/debug configuration to run your tests, and PyCharm allegedly has a "create configuration" dialog box specifically for py.test. But that's the complete extent of their documentation on the subject, and I can't find this alleged dialog box anywhere.
If I right-click the directory in the Project tool window, I'm supposed to see a "Create <name>" menu item, but the only menu item starting with "Create" is "Create Run Configuration". Okay, maybe the documentation is just wrong, and "Create Run Configuration" does sound promising. Unfortunately, the only two items in its submenu are "Unittests in C:\mypath..." and "Doctests in C:\mypath...", neither of which applies -- I'm using neither unittest nor doctest. There is no menu item for py.test.
If I open my test_sample.py and right-click in the editor window, I do get the promised "Create <name>" menu items: there's "Create 'Unittests in test_sa...'...", followed by "Run 'Unittests in test_sa...'" and "Debug 'Unittests in test_sa...'". So again, it's all specific to the unittest framework; nothing for py.test.
If I do try the menu items that say "unittest", I get a dialog box with options for "Name", "Type", a "Tests" group box with "Folder" and "Pattern" and "Script" and "Class" and "Function", etc. This sounds exactly like what's documented as the dialog to add a configuration for Python Unit Test, and not like the "Name" and "Test to run" and "Keywords" options that are supposed to show up in the configuration for py.test dialog. There's nothing inside the dialog to switch which test framework I'm adding.
I'm using PyCharm 1.5.2 on Windows with Python 3.1.3 and pytest 2.0.3. I can successfully run py.test on my tests from the command line, so it's not something simple like pytest not being installed properly.
How do I configure PyCharm to run my py.test tests?

Please go to File| Settings | Tools | Python Integrated Tools and change the default test runner to py.test. Then you'll get the py.test option to create tests instead of the unittest one.

PyCharm 2017.3
Preference -> Tools -> Python integrated Tools - Choose py.test as Default test runner.
If you use Django Preference -> Languages&Frameworks -> Django - Set tick on Do not use Django Test runner
Clear all previously existing test configurations from Run/Debug configuration, otherwise tests will be run with those older configurations.
To set some default additional arguments update py.test default configuration. Run/Debug Configuration -> Defaults -> Python tests -> py.test -> Additional Arguments

I think you need to use the Run/Debug Configuration item on the toolbar. Click it and 'Edit Configurations' (or alternatively use the menu item Run->Edit Configurations). In the 'Defaults' section in the left pane there is a 'py.test' item which I think is what you want.
I also found that the manual didn't match up to the UI for this. Hope I've understood the problem correctly and that helps.

Here is how I made it work with pytest 3.7.2 (installed via pip) and pycharms 2017.3:
Go to edit configurations
Add a new run config and select py.test
In the run config details, you need to set target=python and the unnamed field below to tests. It looks like this is the name of your test folder. Not too sure tough. I also recommend the -s argument so that if you debug your tests, the console will behave properly. Without the argument pytest captures the output and makes the debug console buggy.
My tests folder looks like that. This is just below the root of my project (my_project/tests).
My foobar_test.py file: (no imports needed):
def test_foobar():
print("hello pytest")
assert True
Run it with the normal run command

In pycharm 2019.2, you can simply do this to run all tests:
Run > Edit Configurations > Add pytest
Set options as shown in following screenshot
Click on Debug (or run pytest using e.g. hotkeys Shift+Alt+F9)
For a higher integration of pytest into pycharm, see https://www.jetbrains.com/help/pycharm/pytest.html

It's poorly documented to be sure. Once you get add a new configuration from defaults, you will be in the realm of running the "/Applications/PyCharm CE.app/Contents/helpers/pycharm/pytestrunner.py" script. It's not documented and has its own ideas of command line arguments.
You can:
Try to play around, reverse the script, and see if you can somehow get py.test to accept arguments. It might work; it didn't in the first half hour for me.
Just run "py.test *.py" from a console.
Oddly, you will find it hard to find any discussion as JetBrains does a good job of bombing Google algorithms with its own pages.

find this thread when I hit the same question and found the solution
pycharm version:2017.1.2
go to "Preferences" -> "Tools" -> "Python Integrated Tools" and set the default test runner from right side panel as py.test
solve my problem

I'm using 2018.2
I do Run -> Edit Configurations...
Then click the + in the upper left of the modal dialog.
Select "python tests" -> py.test
Then I give it a name like "All test with py.test"
I select Target: module name
and put in the module where my tests are (that is 'tests' for me) or the module where all my code is if my tests are mixed in with my code. This was tripping me up.
I set the Python interpreter.
I set the working directory to the project directory.

Enable Pytest for you project
Open the Settings/Preferences | Tools | Python Integrated Tools settings dialog as described in Choosing Your Testing Framework.
In the Default test runner field select pytest.
Click OK to save the settings.

Open preferences windows (Command key + "," on Mac):
1.Tools
2.Python Integrated Tools
3.Default test runner

With a special Conda python setup which included the pip install for py.test plus usage of the Specs addin (option --spec) (for Rspec like nice test summary language), I had to do ;
1.Edit the default py.test to include option= --spec , which means use the plugin: https://github.com/pchomik/pytest-spec
2.Create new test configuration, using py.test. Change its python interpreter to use ~/anaconda/envs/ your choice of interpreters, eg py27 for my namings.
3.Delete the 'unittests' test configuration.
4.Now the default test config is py.test with my lovely Rspec style outputs. I love it! Thank you everyone!
p.s. Jetbrains' doc on run/debug configs is here: https://www.jetbrains.com/help/pycharm/2016.1/run-debug-configuration-py-test.html?search=py.test

With 2018.3 it appears to automatically detect that I'm using pytest, which is nice, but it still doesn't allow running from the top level of the project. I had to run pytest for each tests directory individually.
However, I found that I could choose one of the configurations and manually edit it to run at the root of the project and that this worked. I have to manually choose it in the Configurations drop-down - can't right click on the root folder in the Project pane. But at least it allows me to run all tests at once.

There is a PyCharm documentation: Run/Debug Configuration: pytest available as of SEP 2020.

I came across with a bit another case but the same error
(I can't past the error here cuz already fixed it).
Check that you really have pytest installed inside your venv.
I have pytest installed globally and it's confused me.
If pip is saying that pytest is already installed - check what kind of pip it is (global pip or pip of venv).
which python should return the path to your venv python.
Then python -m pip install pytest

To me help next solution:
Go to settings of config
Add new config
Delete old configs

Related

How to run Playwright test from Pycharm in headed mode

I'm using Python & Pytest to write Playwrite tests.
PyCharm is the my IDE.
When I'm debugging a test - I run it from the IDE (by pressing the play icon next to the test). I would like to be able to view the browser during the run in order to debug.
The problem is that when using the 'page' fixture - the default mode is headless.
I know that I can run the test from the CLI and add the flag '--headed', but I want to run headed also by running from the IDE.
Is that possible?
Yes, this is possible!
Just create or add to your pytest.ini file which should go in the root of the working directory.
Here is what the pytest.ini file could look like:
# content of pytest.ini
[pytest]
addopts = -v --browser chromium --headed --slowmo 2000
Having --headed in there is what gives you what you want.
Open Preferences, tab "Python Integrated Tools"
Set Default test runner as pytest
Go to edit configurations in the Run/Debug Configurations
Add a new pytest configuration (plus sign)
Set "Target" as "Custom" and then write whatever flags you want to use in the "Additional Arguments" input field

PyCharm run configuration only shows python tests. How to run it a regular run?

I have cloned a git repository and am trying to run the code on PyCharm IDE. When I try to run it, my usual run option is not available and only run nosetests is available. I read that this is a module to help testing the code, but I don't see an import nosetests or anything like that which helps me to understand why my IDE automatically runs nosetests on this particular code.
Question: How can I run this like a normal code and why I'm seeing this run option instead.
I found multiple questions on how people accidentally changed their IDE setting in a way that all the codes are running using nosetest but not my question. I would appreciate if you can share a link that gives more details on this.
It seems that you do not have a Run Configuration in project that runs the code just tests. In PyCharm go to "Run" -> "Run..." (Shift + Alt + F10) and choose "Edit Configurations..." on the plus sign you can add a new configuration running python code "normally".
It is explained in detail on Jetbrains website:
https://www.jetbrains.com/help/pycharm/creating-and-editing-run-debug-configurations.html?keymap=primary_windows
From what I understand, you are not able to run the py code. You can achieve this easily on the terminal provided within Pycharm, using the commands provided in the project README.
Alternatively, if you want to run it using the GUI, you can edit the Run Configuration by clicking the dropdown near the Run icon at the top.
For further information please head out to https://www.jetbrains.com/help/pycharm/creating-and-editing-run-debug-configurations.html?keymap=primary_windows

Configuring IDE for Nosetests for python: How do I get script option?

My friend has following setup with his Nose tests. Basically, he sees "Script" option for test but I do not.
IDE information Pycharm CE:2017.2
Friend setup with test/script option
My set up is as follows.
My Setup without test option
Question: How do I get "test" instead of target and "Script" instead of python/path/custom.

How to use robot framework with python in eclipse and How to run the Robot framework tests in eclipse using python interpreters?

I have installed robot framework plugin into my eclipse.
I have added python plugins and interpreter in the path, python files are executing successfully.
I have added built-in library index file in the project, created robot text file and i want to execute the test but i right click on the test and try to run as, it is not giving the run configurations. I tried F9 for execution but it is not working.
I think there is the separate set up required for running the robot tests in eclipse.
my project we don't have access to Maven and client doesn't want Maven.
Please let me know how to execute the robot tests in eclipse.
Thanks
You can use RED (Robotframework Editor), an extension for Eclipse available in the Eclipse Marketplace. Once you installed, simply go to "Run -> Run configurations..." and configure a run profile under "Robot". Eclipse will execute the following command on your test:
/usr/bin/python -m robot.run
You can check the logs in the "Message Log" view, and test results in the "Execution View".
Here's how I setup my Eclispe to run Pybot for Windows 7. You have to setup "External Tools", I don't have a high enough reputation to post images so see the steps below. Once you've setup an external tools configuration you'll use "Run-->External Tools-->Pybot".
HTH,
From the Eclipse tool bar:
Run-->External Tools-->External Tools Configurations...
From the "External Tools configurations" pop-up select "Program", then press the New button
Fill in the following setting on the definition screen:
Name: Pybot (or a name of your choosing)
Location: C:\Python27\Scripts\pybot.bat (path to your pybot.bat file)
Working Directory: C:\workspace_42 (your Eclipse workspace)
Arguments: "${selected_resource_loc}"
If you want to execute Python's flavour of Robot Framework you must use either command line or the Robot IDE (RIDE). To run your tests from the command line you must:
1. Open the terminal
2. Navigate to the appropriate directory
3. Use the pybot command and then specify the folder/file containing your tests
eg.
pybot mytestfolder
pybot myfirsttest.txt

Interactive debugging with nosetests in PyDev

I'm using PyDev ( with Aptana ) to write and debug a Python Pylons app, and I'd like to step through the tests in the debugger.
Is it possible to launch nosetests through PyDev and stop at breakpoints?
Here is what i do to run nosetests using eclipse Pydev (Hope this will help you).
first of all i create a python script and i put it in the root of my package directory :
--Package
|
| -- runtest.py
|
| -- ... (others modules)
and in runtest.py i put:
import nose
nose.main()
now i go to in the menu Run -> Run configurations and i create a new configuration of Pydev Django i choose my package and put runtest.py in the main Module , next i go to arguments tab in the same widget and i put in Program arguments the path to my project and different arg to pass to the script example:
/home/me/projects/src --with-doctest # Run doctests too
now after clicking on Apply i can run this configuration .
For debugging you can run this configuration in debug mode and put your break point anywhere in your code and you can use the terrific debug widget to do several action : step into, to see vars ...
N.B : for doctests sadly i don't think you can put breakpoint in the line of doctest but what you can do is to put your breakpoint in the def of the function that is called by the doctest and like that you can use the debug mode .
Try import pydevd; pydevd.settrace() where would like a breakpoint.
I got this working, somewhat - that is, I don't have breakpoints and stepping working but I do get PyDev to run the tests and show the results in the PyUnit view.
When you run the unit test you'll have to override the test runner to use "nose" and command line arguments "--with-pylons=path/to/test.ini" in the arguments tab of the run configuration. For example I set it to "--with-pylons=../../test.ini". Unfortunately I have to set this up separately for each test I run, I haven't found a way to put a variable or project path in there.
Also, unfortunately, I haven't been able to get breakpoints working. I tried patching as recommended in http://pydev.blogspot.ca/2007/06/why-cant-pydev-debugger-work-with.html and its comments to no avail. YMMV.
In DecoratorTools-1.8-py2.7.egg/peak/util/decorators.py in decorate_assignment(), replace:
oldtrace = [frame.f_trace]
with
oldtrace = [sys.gettrace()]

Categories