how to debug twisted trial unittest in pycharm - unit-testing

I am new to twisted and I have a twisted unit test in python, and I want to debug in pycharm with trial.
I can run the tests in command line fine (for e.g. like :~ nathan$ trial smoke_tests ) but would like to step through the test in an IDE
in another question
How debuging twisted application in PyCharm
It has been suggested that "configure the "Script" setting to point to that twistd" . so for 'trial' I tried pointing to /Library/Python/2.7/site-packages/twisted/trial/runner.py , but that fails.

Script
Assuming your working directory is /home/myself/mypythonproject/myworkingdirectory ,
Create a python file in your working directory with name trial_try.py. This is a copy of /usr/local/bin/trial. So use a copy of the version you have.
import os, sys
try:
import _preamble
except ImportError:
try:
sys.exc_clear()
except AttributeError:
# exc_clear() (and the requirement for it) has been removed from Py3
pass
# begin chdir armor
sys.path[:] = map(os.path.abspath, sys.path)
# end chdir armor
sys.path.insert(0, os.path.abspath(os.getcwd()))
from twisted.scripts.trial import run
run()
Configuration
Create a new Run Configuration in Pycharm.
for Script Enter
/home/myself/mypythonproject/myworkingdirectory/trial_try.py
for Parameters Enter you Parameters that you would use when running trial on the command line
for e.g. test_smoke
for Working directory Enter
/home/myself/mypythonproject/myworkingdirectory
You should be all Set !

Related

MySQLdb import works from command line but crashes in PyCharm

I am using a Python 2.7 virtualenv with the MySQLdb package installed.
If I run Python from the command line and execute import MySQLdb, this works without error. If I run it from the PyCharm terminal, however, I get an error:
ImportError: libmysqlclient.so.20: cannot open shared object file: No such file or directory
The same pattern occurs if I execute a file test.py containing the line import MySQLdb. It works when executed from the command line and crashed when executed from PyCharm.
I have googled the error and it seems that uninstalling and reinstalling MySQLdb could fix it. But I would like to understand why the error only occurs in PyCharm.
I have made sure that both the command line and the PyCharm terminal use
the same virtual environment (by checking sys.executable)
the same working directory (by checking os.getcwd())
the same path (by checking sys.path)
I have also checked that PYTHONPATH is undefined.
What other difference could there be?
You have to point pycharm to your virtualenv. Go to settings -> project interpreter and give pycharm the path to your python executable. Once there it should work. Note if you have a hybrid WSL/windows setup you will need one virtualenv for WSL and a separate virtualenv for windows/pycharm.

Configure a django test with no migrations in pyCharm

I am new to both django and pycharm! I can run the tests in my code on terminal using:
python manage.py test Repo/tests/testUnit1.py --failfast -n
and it works! Recently, I tried to use pycharm (professional) to run and debug the tests. The problem is that when I specify the option --nomigrations it gives the following error:
Usage: /Applications/PyCharm.app/Contents/helpers/pycharm/django_test_manage.py test [options]
[path.to.modulename|path.to.modulename.TestCase|path.to.modulename.TestCase.test_method]...
Discover and run tests in the specified modules or the current directory.
/Applications/PyCharm.app/Contents/helpers/pycharm/django_test_manage.py: error: no such option: --nomigrations
I found similar question here but it suggests the same thing that I have already tried. Does this happen because the test unit and the code that I want to test are not in the same folder? How can I run a test in pycharm without migrations?
In case this saves someone else some time here is how to set it up (took me awhile to figure out the first couple steps)...
Select Edit configurations
Create a new Python configuration (not a Django tests configuration)
In Script put manage.py
In Script parameters put test --nomigrations <optional test labels>
Optionally may need to specify a Working directory, depending on how PyCharm is started
As always, make sure your Environment variables, Python interpreter and Interpreter options are set to your project
This is on PyCharm 2016.2.3 and Django 1.8.9 with django-test-without-migrations installed
I figured out my mistake. I edited Python Run/Debug configuration and passed manage.py to Script. Also, I pasted the path that I used to use on command terminal (plus --failfast -n at the end) in Script parameters and it starts working!
Rather than use the configuration menu, I just edit the source.
You'll see when running tests that PyCharm that it uses its work test runner, something like
/Applications/PyCharm.app/Contents/helpers/pycharm/django_test_manage.py
Opening it up, I add the functional part that's taken from django-test-without-migrations
# added this class
class DisableMigrations(object):
def __contains__(self, item):
return True
def __getitem__(self, item):
return "notmigrations"
# ... right before this built-in
class PycharmTestCommand(Command):
def get_runner(self):
TEST_RUNNER = 'django_test_runner.run_tests'
test_path = TEST_RUNNER.split('.')
#....
Then, inside of the PycharmTestCommand.handle() method:
def handle(self, *test_labels, **options):
# Add this line
settings.MIGRATION_MODULES = DisableMigrations()
# That's it!
# ....
Now it works on all your projects, whether or not they have that lib installed. I still install the lib in case I need to run tests outside of PyCharm.

redirecting python import to another module

I'm working on a large open source Python project, which has modules used by both the project and other projects. The goal is to move some of these modules out to a new "library" project that can then be imported by the original project and other projects.
To make this transition smooth, the thought was to copy the modules over to the new project, and have the original project then use the new import. However, to allow other project to have time to migrate later, the thought was to have the original module redirect the import.
For example, the usage is like this in repo 'neutron' (other projects could do the same):
cat neutron/consumer.py
from neutron.redirected import X
print(X)
The in the new 'neutron_lib' project created, the module looks like this (the same as what the original was in project 'neutron'):
cat ../neutron-lib/neutron_lib/redirected.py
X = 5
In the 'neutron' project, I'm trying to do this as the redirect module:
cat neutron/redirected.py
import neutron_lib.redirected
import sys
sys.modules['neutron.redirected'] = neutron_lib.redirected
When I run pylint, it gives these errors:
************* Module neutron.redirected
E: 1, 0: No name 'redirected' in module 'neutron_lib' (no-name-in-module)
************* Module neutron.consumer
E: 1, 0: No name 'X' in module 'neutron.redirected' (no-name-in-module)
If I run this, it runs fine, and consumer.py prints '5'. If I use ipython and load consumer.py, I can see 'X' in dir() output.
Any idea why I'm getting this pylint error? Is it a false error? Is there a way to override it?
Looks like, when running under tox, I can add the following to .pylintrc to hide the errors/warnings
no-name-in-module
nonstandard-exception
When I run pylint it passes now, as does running the Unit tests. Just wish I understood why I'm getting these errors/warnings though.

python import cx_Oracle error in command window

I'm having trouble running some codes that import cx_Oracle in command line, though the same codes work in console. Is there anything else I will need to set up in order to get this to work via command line please?
Saved just one line of code "import cx_Oracle" as test.py.
Run this line in ide (Spyder), iPython Notebook => no issues
run this by opening a command line window from the same folder the .py file is saved in, and run python test.py and encounter the below:
import cx_Oracle
ImportError: DLL load failed: %1 is not a valid Win32 application.
Not sure if there is anything additional I will need to set up to run cx_Oracle via command line? Have tried all the suggestions on setting PATH, ORACLE_HOME, re-installing but could not get this to work. versions that i'm using are
Python: 2.7
cx_Oracle: cx_Oracle-5.1.3-11g.win-amd64-py2.7
instant client: 12.1.0.0
Windows: 7 Enterprise
I also found this kind of problem.
Look at "not a valid Win32 application" this sentence, so I decide to change cx_Oracle to cx_Oracle-5.1.3-11g.win-32-py2.7. Luckly, it does work.

Setting current directory for Interactive Console in pydev (Eclipse) at console startup

I want to start an interactive console in pydev from project directory, in order to import an app. I tried to use os.chdir at startup from Window->Preferences->PyDev->Interactive Console->Initial interpreter commands.
I read https://docs.djangoproject.com/en/dev/ref/settings/ searching for an entry to set path but I didn't find anything.
Thanks
Edit: I had to import module first in order to import app
Strange, I must say that after changing the initial interpreter commands to be:
import sys; print('%s %s' % (sys.executable or sys.platform, sys.version))
import os;os.chdir('c:\\')
Later doing (in the shell):
import os.path
os.path.abspath('.')
Does show the expected path... Aren't you getting that? What do you get when you do the abspath('.') in the shell after the startup?