python 在每个测试方法之前清理 django 中的数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/434700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cleaning up a database in django before every test method
提问by Marcin
By default when Django runs against sqlite backend it creates a new in memory database for a test. That means for every class that derives from unittest.TestCase, I get a new database. Can this be changed so that it is cleared before every test method is run?
默认情况下,当 Django 针对 sqlite 后端运行时,它会为测试创建一个新的内存数据库。这意味着对于从 unittest.TestCase 派生的每个类,我都会得到一个新数据库。可以更改此设置,以便在运行每个测试方法之前将其清除吗?
Example: I am testing a manager class that provides additional abstraction on top of Django persistent objects. The code looks more-less like that
示例:我正在测试一个管理器类,它在 Django 持久对象之上提供额外的抽象。代码看起来更像那样
class TestForManager(unittest.TestCase):
def testAddingBlah(self):
manager = Manager()
self.assertEquals(manager.getBlahs(), 0)
manager.addBlah(...)
self.assertEquals(manager.getBlahs(), 1)
def testAddingBlahInDifferentWay(self):
manager = Manager()
self.assertEquals(manager.getBlahs(), 0)
manager.addBlahInDifferentWay(...)
self.assertEquals(manager.getBlahs(), 1)
Now, the first assertion of second test fails, because the state of the database is preserved between test calls and there already is an instance of Blah
in the database.
现在,第二个测试的第一个断言失败了,因为数据库的状态在测试调用之间保留,并且数据库中已经有一个实例Blah
。
回答by Marcin
As always, solution is trivial: use django.test.TestCase
not unittest.TestCase
. And it works in all major versions of Django!
与往常一样,解决方案很简单:使用django.test.TestCase
not unittest.TestCase
。它适用于所有主要版本的 Django!
回答by Rory
You can use the tearDown
method. It will be called after your test is run. You can delete all Blahs there.
您可以使用该tearDown
方法。它将在您的测试运行后调用。您可以删除那里的所有废话。
回答by vestronge
For clearing non-default databases, add multi_db = True
in the class
要清除非默认数据库,请multi_db = True
在类中添加
eg
例如
class MyTestCase(django.test.TestCase)
multi_db = True
def test_one(self):
self.assertTrue(True)
回答by Mamun Hasan
Make them in two different functions both are not test function. Finally call the dependent functions from one test function.
使它们在两个不同的函数中都不是测试函数。最后从一个测试函数调用依赖函数。
回答by S.Lott
Why not do the following? This accomplishes what you need without a significant change to your code.
为什么不做以下事情?这可以完成您需要的操作,而无需对您的代码进行重大更改。
class TestOneForManager(unittest.TestCase):
def testAddingBlah(self):
manager = Manager()
self.assertEquals(manager.getBlahs(), 0)
manager.addBlah(...)
self.assertEquals(manager.getBlahs(), 1)
class TestTwoForManager(unittest.TestCase):
def testAddingBlahInDifferentWay(self):
manager = Manager()
self.assertEquals(manager.getBlahs(), 0)
manager.addBlahInDifferentWay(...)
self.assertEquals(manager.getBlahs(), 1)
Edit. The "reset on TestCase" feature gives you complete control.
编辑。“在测试用例上重置”功能让您可以完全控制。
Many test methods in a single TestCase are good when you have test cases that don't interfere with each other.
Few test methods in a single TestCase are good when you have test cases that interfere with each other.
当您拥有互不干扰的测试用例时,单个 TestCase 中的许多测试方法都很好。
当您的测试用例相互干扰时,单个 TestCase 中的测试方法很少是好的。
You can choose which model applies to your tests by grouping your test methods in one or many TestCases. You have total and complete control.
您可以通过将您的测试方法分组到一个或多个测试用例中来选择适用于您的测试的模型。您拥有完全和完全的控制权。