简体   繁体   English

在PyCharm中运行Django应用测试

[英]Running Django apps tests in PyCharm

I am having a hard time trying to configure tests running in PyCharm. 我很难配置在PyCharm中运行的测试。

I want to run tests for my custom django apps, so my configuration looks something like this: 我想为自定义django应用程序运行测试,因此我的配置如下所示:

选项字段中的Django应用

It works okay mostly (tests run, succeed), though it doesn't let you re-run individual tests and re-run failed ones - it always runs all tests for specified applications (common and authorization). 尽管它不允许您重新运行单个测试和重新运行失败的测试,但大多数情况下都可以正常运行(测试运行成功),它始终为指定的应用程序运行所有测试(通用和授权)。

The manual says I should put django application names in "Target" field like this: 手册说我应该将django应用程序名称放在“目标”字段中,如下所示:

目标字段中的Django应用

But whenever I do it, my tests fail to run with weird errors: sometimes it cannot import some modules, though they are definitely accessible, sometimes there are a lot of NoReverseFound exceptions, though none of them are actually present in the code. 但是,每当执行此操作时,我的测试都将无法运行,并出现奇怪的错误:有时无法导入某些模块,尽管它们确实可以访问,但有时会出现很多NoReverseFound异常,尽管实际上代码中都没有异常。

I suppose I am configuring something wrong, but I cannot understand what. 我想我配置错了,但是我不明白是什么。 I am running the latest version of PyCharm and one of the 1.5.x versions of Django (some legacy code from back in the day I had to maintain) 我正在运行最新版本的PyCharm和Django的1.5.x版本之一(我不得不维护时的一些旧代码)

UPD : If I put authorization.UserApiTestCase.test_login in target - it works great, authorization.UserApiTestCase works too, but putting just appname ( authorization ) won't work and will produce NoReversrMatch or import errors... leaving Target empty will work, too, though it will run even iinternal Django tests and that's not what I need - I just want to run all my apps' tests (or tests from specific apps). UPD :如果我在目标中放置了authorization.UserApiTestCase.test_login它工作得很好, authorization.UserApiTestCase可以工作,但是仅将appname( authorization )放不起作用,并且会产生NoReversrMatch或导入错误……将Target留空将是可行的,尽管它甚至可以运行内部Django测试,但这也不是我所需要的-我只想运行我所有应用程序的测试(或特定应用程序的测试)。

If your project structure in PyCharm looks similar to this: 如果您在PyCharm中的项目结构看起来与此类似:

myproject
├── README.md
├── myproject
│   ├── app1
│   │   └── test.py
│   ├── app2
│   │   └── test.py
│   ├── app3
│   │   └── test.py
│   └── settings.py
└── requirements.txt

Then you may need to add the second level myproject folder as a "Sources Root" 然后,您可能需要将第二级myproject文件夹添加为“源根目录”

"Sources Root" folders are indicated by a blue folder icon in the "Project" window “源根目录”文件夹由“项目”窗口中的蓝色文件夹图标指示

To add a "Sources Root" go to Preferences -> Project:MyProject -> Project Structure 要添加“源根目录”,请转到“首选项”->“项目:MyProject”->“项目结构”

Also check off "add source roots to PYTHONPATH" in the "Run/Debug Configurations". 还要在“运行/调试配置”中选中“将源根添加到PYTHONPATH”。

After that you should be able to run tests with specified targets. 之后,您应该可以运行具有指定目标的测试。

As it turned out, it's a bug related to older versions of Django. 事实证明,这是与旧版Django相关的错误。 Upgrading to latest (1.9.2 at the moment) resolved the problem. 升级到最新版本(目前为1.9.2)可以解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM