2016-11-01 38 views
2

我對django和pycharm都是新手!我可以在終端上使用我的代碼運行測試:在pyCharm中配置沒有遷移的django測試

python manage.py test Repo/tests/testUnit1.py --failfast -n 

它的工作原理!最近,我試圖使用pycharm(專業版)來運行和調試測試。問題是,當我指定的選項--nomigrations它提供了以下錯誤:

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 

我發現類似的問題here,但它表明,我已經嘗試過同樣的事情。這是否會發生,因爲測試單元和我想測試的代碼不在同一個文件夾中?我如何在沒有遷移的情況下在pycharm中運行測試?

回答

2

我想出了我的錯誤。我編輯Python Run/Debug configuration並通過manage.pyScript。另外,我在Script parameters上粘貼了我以前在命令終端上使用的路徑(加上--failfast -n結尾),它開始工作!

3

在這種情況下,節省了別人一些時間在這裏是如何設置它(我花了一段時間來弄清楚前幾步驟)...

  • 選擇編輯配置

  • 創建一個新的Python 配置(不是Django的測試配置)

  • 腳本manage.py

  • 腳本參數test --nomigrations <optional test labels>

  • 可選可能需要指定一個工作目錄,取決於PyCharm是如何開始

  • 一如既往,確定你的環境變量,Python解釋器個解釋選項設置爲你的項目

這是PyCharm 2016年2月3日和Django的1.8.9與django-test-without-migrations安裝

+0

謝謝!比我的答案更可讀! – MhFarahani

1

而不是使用配置菜單,我只是編輯源。

運行PyCharm,它使用它的工作測試運行測試,東西的時候像

/Applications/PyCharm.app/Contents/helpers/pycharm/django_test_manage.py 

打開它你會看到,我添加了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('.') 
    #.... 

然後,內PycharmTestCommand.handle()方法:

def handle(self, *test_labels, **options): 
    # Add this line 
    settings.MIGRATION_MODULES = DisableMigrations() 
    # That's it! 
    # .... 

現在它適用於所有你的親無論他們是否安裝了該lib。如果我需要在PyCharm之外運行測試,我仍然安裝lib。