Django 测试装置:如何处理涉及时间的测试数据?
目前我正在一个可重用的 Django 应用程序上进行 TDD。一项测试应确保我的视图仅返回发布日期为过去的文章。我对 Django 测试还很陌生。到目前为止,我已经学会了如何使用 .json
文件作为我的测试类的固定装置。
但是,对于此测试,我不想插入 1000 年后的发布日期。毕竟其他物种可能会发现我们古老的互联网,请查看我的来源并想知道为什么我的测试失败:)还有哪些其他方法可以解决这个问题?静态 .json
文件似乎有点难以维护,而且应用程序也在不断增长......在我的测试中模拟 datetime.datetime.now()
方法感觉很乏味以及。
必须有一种简单的方法可以在测试运行之前动态生成 .json
固定装置,并且从现在起总是有 2 天作为我的一些条目的发布日期......
Currently I am doing TDD on a reusable Django app. One test should make sure that my view only returns articles that have a publish-date that is in the past. I am quite new to testing in Django. So far I have learned how to use .json
files as fixtures for my test classes.
However for this test I do not want to insert publish dates that are 1000 years in the future. After all another species might discover our ancient internet, check out my source and wonder why my test fails :) What other approaches are there to solve this problem? Static .json
files seem to be a bit hard to maintain as well as the application grows... mocking the datetime.datetime.now()
method in my tests feels tedious as well.
There must be an easy way to produce the .json
fixture on the fly before the test runs and always have 2 days from now as the publish-date for some of my entries...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以尝试对日期时间函数进行子类化(请参阅 “在 Django 中使用 Mock 对象来测试当前日期”)。
You could try subclassing the datetime functions (see "Using Mock objects in Django for testing the current date").
嘲笑日期时间也是我的第一个想法,但正如 LaundroMat 在他的博客文章中指出的那样,这是相当 hacky 和糟糕的。
我想出了另一个解决方案:
我只是放弃了 .json 装置。我什至不知道为什么 Django 鼓励使用它们。它们看起来很丑而且无法维护。
相反,我将模块 test_data.py 添加到我的装置文件夹中。该模块导入我的模型并定义了一些为我创建测试数据的好方法。在我的tests.py 中,我删除了fixtures = ['some_json_file'] 行并添加了setUp() 方法。此方法从我的 test_data.py 模块执行我的“动态装置”。
这是如此简单和明显,我想知道这种方法是否有什么问题。如果没有人对此解决方案发表评论,我将在几周左右将其标记为已接受......
Mocking datetime was my first thought as well but as LaundroMat pointet out in his blog post himself, this is rather hacky and bad.
I came up with another solution:
I just gave up on .json fixtures. I don't even know why Django encourages to use them. They look ugly and they are impossible to maintain.
Instead I added a module test_data.py to my fixtures folder. That module imports my models and defines some nice methods that create test-data for me. In my tests.py I dropped the fixtures = ['some_json_file'] line and added a setUp() method instead. This method executes my 'dynamic fixtures' from my test_data.py module.
This is so simple and obvious that I wonder if there is anything wrong with this approach. If no one comments on this solution, I will mark is as accepted in a couple of weeks or so...
您可以尝试使用django动态夹具,它会自动为您填充日期/时间属性。
You can try using django dynamic fixture, it will fill date/time properties automatically for you.
创建测试数据
事实上,使用 Python 创建测试数据要灵活得多。
有几个包支持此类功能,例如
这两者使用完全不同的方法。看看并选择您更喜欢的一个。
您还可以从头开始编写数据生成函数。如果你仔细地划分你的逻辑,这并不会太麻烦。
处理时间
对于涉及时间的测试,正确的方法确实是嘲笑——但不要自己做,使用一个好的库。例如,参见
甚至还有一个 pytest 插件 。
Creating test data
Indeed it is far more flexible to create your test data with Python.
There are several packages that support such things, for instance
These two use fairly different approaches. Have a look and pick the one you like more.
You can also write data generation functions from scratch. If you partition your logic carefully, this is not overly cumbersome.
Handling time
For tests involving time, the right approach is indeed mocking -- but don't do it yourself, use a nice library. See for instance
There is even a pytest plugin for it.