在每个Django测试之前初始化MEDIA_ROOT [英] Initializing MEDIA_ROOT before each Django Test

查看:209
本文介绍了在每个Django测试之前初始化MEDIA_ROOT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要我的Django测试来创建和修改媒体文件。所以,很像Django测试使用数据库,我想在运行每个测试之前设置一个空的MEDIA_ROOT文件夹。



我想我将创建一个临时文件夹点MEDIA_ROOT给它。但是,我无法确定在哪里放置代码。在这个例子中,一个特别的 Runner 已创建。跑步者设置媒体根并撕下它。



不幸的是,在第一次测试之前调用了一个 setup_test_environment 函数运行,而不是每次运行测试。



我尝试创建一个 FileSystemTestCase 设置的类文件系统在其 setUp 函数中,并且具有我所有的测试用例。虽然这样做,它要求每个写入测试用例的人记住调用我的 setUp 方法,因为它不会自动调用。


$ b $通常我不会打扰这一点,但是忘记调用父级setUp方法的代价可能非常高 - 如果有人忘记了调用,并且测试意外地在现场系统上运行,则会发生不好的事情。



编辑:我发现的临时解决方案是实现我自己的跑步者和基础 TestCase code>。两者都设置了一个临时的MEDIA_ROOT,所以如果有人忘记调用我的 setUp 方法,测试将在以前的测试的临时文件夹中运行,或者由亚军。这可能会导致测试失败,但不会破坏实时数据。



我希望有一个更优雅的解决方案。

解决方案

在我看来,您正在尝试解决两个问题:


  1. 当测试人员做正确的事情(即继承自测试类并调用您的 setUp()),允许测试独立运行(关于 MEDIA_ROOT )。

  2. 当测试人员意外地做错了事情时,让测试人员不要混淆实际数据。

鉴于此,我认为双管齐下的做法是有道理的。您的 setUp()解决问题1.但是,在测试运行器中设置 MEDIA_ROOT 隐藏了您的测试人员做错了事相反,我只会专注于保护数据:例如,您可以将 MEDIA_ROOT 设置为。这将屏蔽 MEDIA_ROOT 中的真实数据;如果不使用您的 setUp(),测试人员可能会看到错误;并减少代码重复。



更健壮的方法是编写自己的测试运行程序,在每次测试之前进行设置(以Django处理数据库的方式建模)但是对于您的需要可能会过度使用。


I want to my Django tests to create and modify media files. So, much like Django tests do with databases, I want to set up an empty MEDIA_ROOT folder before each test is run.

I figured I'll create a temporary folder and point MEDIA_ROOT to it. However, I can't figure out where to put the code that does this. In this example, a special Runner is created. The runner sets up the media root and tears it down.

Unfortunately, setup_test_environment is called once before the first test function is run, and not every time a test is run.

I tried creating a FileSystemTestCase class that sets up the file system in its setUp function, and have all my test cases derive from it. While this works, it requires every person who writes a testcase to remember to call my setUp method, as it's not called automatically.

Usually I wouldn't bother with this, but the cost of forgetting to call the parent setUp method can be very high - if someone forgets the call and the tests are accidentally run on a live system, bad things will happen.

EDIT: The temporary solution I've found was to implement both my own runner and a base TestCase. Both set up a temporary MEDIA_ROOT, so if someone forgets to call my setUp method, the test will run in the temporary folder of the previous test, or the one set up by the runner. This can cause tests to fail, but will not ruin live data.

I'm hoping for a more elegant solution.

解决方案

It seems to me that you're trying to address two separate issues:

  1. Allow tests to run independently (with regard to MEDIA_ROOT) when testers do the right thing (i.e. inherit from your test class and call your setUp()).
  2. Keep testers from messing up real data when they accidentally do the wrong thing.

Given that, I think a two-pronged approach makes sense. Your setUp() solves problem 1. Setting up MEDIA_ROOT in the test runner, though, hides the fact that your testers have done the wrong thing. Instead I would just focus on protecting the data: for example, you could set MEDIA_ROOT to None. That would shield the real data in MEDIA_ROOT; make it more likely that the tester will see an error if they don't use your setUp(); and reduce code duplication.

A more robust approach would be to write your own test runner that does the setup before each test (modeled after the way Django handles the database), but that may be overkill for your needs.

这篇关于在每个Django测试之前初始化MEDIA_ROOT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆