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
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:
if __name__ == '__main__':
blockRun
After that, you should be able to run or debug the file as expected.
Using PyCharm Community Edition 2017.2 EAP. Here is how I did it:
Run
menu select Run...
. (Or use its keyboard shortcut)edit configuration...
, my_script_name
, and Unittests in my_script_name.py
. Click on the right arrow of my_script_name
.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.
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
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.
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