I have a file and it has bunch of methods starting with prefix "test". Now I want to run this file and it has a __name__ == "__main__"
construct which sets it up. However, when I right click or go to run menu bar, It (Pycharm) only shows me "Run Unittests in xxx". This is very annoying behaviour.
Can someone please tell me how to avoid this.
If you right-click inside the 'if name == "main"' block, it will show you the regular "Run script" option instead of "Run unit test". After that, you can save the created run configuration and use it to run the script.
One detail missing from @yole’s answer is that you need to delete any existing configurations associated with the file before you can right-click and get it to run/debug. So, the full procedure is:
- Delete any existing configurations for that file by going to ‘Edit Configurations’
- Right click inside the
if __name__ == '__main__':
block
- Choose
Run
- Save the configuration
After that, you should be able to run or debug the file as expected.
I couldn't get @yole's answer to work but i did manage by going to
run > edit configurations > new configuration (the plus sign) > select python,
now input a name (mine was 'stop the bloody unittest') and select the correct script
Using PyCharm Community Edition 2017.2 EAP. Here is how I did it:
- From the
Run
menu select Run...
. (Or use its keyboard shortcut)
- A popup menu appears that should contain at least three entries:
edit configuration...
, my_script_name
, and Unittests in my_script_name.py
. Click on the right arrow of my_script_name
.
- If the configuration is not saved, you should be able to see a
save configuration
option in the arrow menu. Choose it.
If the configuration changed again, follow the same steps but this time choose run
in the last step.
As to me, "If you right-click inside the 'if name == "main"' block" will not work.
For example if you want to run "dlib_fun_test.py", you should ‘Edit Configurations’ , remove it from "python tests", and then add it to the above "Python".
The below is my configure:
Or you can also run the script, in bash console by python just_test.py.
I had the same problem, deleted my .idea
folder in the directory of the project I was working on and was able to run my tests normally again:
PyCharm 2018.2.4 (Community Edition)
Build #PC-182.4505.26, built on September 19, 2018
JRE: 1.8.0_152-release-1248-b8 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 10 10.0
P.S. You can your version of PyCharm by going to Help -> About