在PyCharm中运行Django应用程序测试

时间:2022-11-19 10:22:29

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应用程序运行测试,所以我的配置看起来像这样:

在PyCharm中运行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应用程序名称放在“Target”字段中,如下所示:

在PyCharm中运行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和1.5.x版本的Django之一(我必须维护的那天后面的一些遗留代码)

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(授权)将不起作用并将产生NoReversrMatch或导入错误...让Target为空将起作用,虽然它甚至可以运行我的内部Django测试,但这不是我需要的 - 我只想运行我所有应用程序的测试(或来自特定应用程序的测试)。

2 个解决方案

#1


3  

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”

"Sources Root" folders are indicated by a blue folder icon in the "Project" window

“Sources Root”文件夹由“Project”窗口中的蓝色文件夹图标表示

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.

之后,您应该能够使用指定的目标运行测试。

#2


0  

As it turned out, it's a bug related to older versions of Django. Upgrading to latest (1.9.2 at the moment) resolved the problem.

事实证明,这是与旧版Django相关的错误。升级到最新版本(目前为1.9.2)解决了这个问题。

#1


3  

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”

"Sources Root" folders are indicated by a blue folder icon in the "Project" window

“Sources Root”文件夹由“Project”窗口中的蓝色文件夹图标表示

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.

之后,您应该能够使用指定的目标运行测试。

#2


0  

As it turned out, it's a bug related to older versions of Django. Upgrading to latest (1.9.2 at the moment) resolved the problem.

事实证明,这是与旧版Django相关的错误。升级到最新版本(目前为1.9.2)解决了这个问题。