MhFarahani
MhFarahani

Reputation: 970

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?

Upvotes: 2

Views: 2387

Answers (3)

Marcel Chastain
Marcel Chastain

Reputation: 2131

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.

Upvotes: 2

FlyDanoFly
FlyDanoFly

Reputation: 279

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

Upvotes: 4

MhFarahani
MhFarahani

Reputation: 970

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!

Upvotes: 2

Related Questions