Django测试完成后如何保存测试数据? [英] How can I keep test data after Django tests complete?

查看:215
本文介绍了Django测试完成后如何保存测试数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django 1.8和文档说要使用-keepdb 保存测试数据库。

I am using Django 1.8 and the docs say to use --keepdb to save the test database.

我正在这样做,而且数据库在那里,但是每次我看到它时,它都是空的,里面没有数据。

I am doing that and the database is there but every time I see it, it is empty and has no data in it.

有什么方法可以保存它,以便查看其中的内容

Is there any way that I can preserve that so that I can see what's in there?

推荐答案

您的所有代码都在数据库事务中运行,在每次测试结束时都会回滚。

All of your code is running within database transactions, which get rolled back at the end of each test.

来自 Django测试文档


以下是django.test.TestCase子类的示例,django.test.TestCase是 unittest.TestCase 在事务内运行每个测试以提供隔离:

Here is an example which subclasses from django.test.TestCase, which is a subclass of unittest.TestCase that runs each test inside a transaction to provide isolation:

此隔离包括意味着您在测试中 所做的任何事情都会在下一个测试开始之前回滚。

This "isolation" means that anything you do inside of the test will be rolled back before the next test starts.

相反,您想使用Python的类 unittest.TestCase

Instead, you want to use Python's class unittest.TestCase.

Django文档中的另一句话:

Another quote from the Django docs:


使用 unittest.TestCase 避免了在事务中运行每个测试和刷新数据库的成本,但是如果您的测试与数据库交互,则其行为将根据测试运行程序的顺序而变化执行它们。这可能会导致单元测试在单独运行时通过,但在套件中运行时会失败。

Using unittest.TestCase avoids the cost of running each test in a transaction and flushing the database, but if your tests interact with the database their behavior will vary based on the order that the test runner executes them. This can lead to unit tests that pass when run in isolation but fail when run in a suite.

只要可以保证测试不会失败, t破坏彼此的数据,您可以放心地使用此类代替Django的测试用例。

As long as you can guarantee that your tests won't clobber each other's data, you can safely use this class instead of Django's test case.

这篇关于Django测试完成后如何保存测试数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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