django 测试 - 如何避免 ForeignKeyViolation
django test - how to avoid the ForeignKeyViolation
我正在努力使用具有外键的对象使用 Django 创建测试。测试 运行 时 运行 单独时没有错误,但是当 运行 全部时,我在 teardown
阶段得到 ForeignKeyViolation
(我不明白它是做什么的,但找不到任何相关信息)。
启动测试套件时,我创建了一堆这样的虚拟数据:
def setUp(self) -> None:
create_bulk_users(5)
create_categories()
create_tags()
create_technologies()
self.tags = Tag.objects.all()
self.categories = Category.objects.all()
self.technologies = Technology.objects.all()
我需要帮助解决的问题是:
teardown
阶段究竟做了什么?有详细的文档吗?
- 我应该如何构建测试以避免
ForeignKeyViolation
问题?
拆解阶段具体做什么?有详细的文档吗?
我找不到任何关于它的详细文档。我所发现的只是阅读实际代码。拆卸会取消测试期间对数据库所做的任何操作。
The way TestCases work (broadly) is this:
1. SetUp method runs **before** every test
2. individual test method is run
3. TearDown method runs **after** every test
4. Test database is flushed
我应该如何构建测试以避免 ForeignKeyViolation 问题?
我的错误是我导入了一个用于创建虚拟数据的函数。
导入的模块是 运行ning 一个函数来填充一些主要数据。问题是在 setUp 方法中不是 re运行 each,因为在导入文件时它 运行 只是一个。
# imported module
# will delete all tags after running first test but
# the reference to the old tags will persist
tags = FakeTags.create_bulk();
def create_bulk_articles():
article = FakeArticle.create()
article.tags.add(tags)
我只是使用导入函数中的故障函数修复了它。
# imported module
# tags are created each time before running the tests
def create_bulk_articles():
tags = FakeTags.create_bulk();
article = FakeArticle.create()
article.tags.add(tags)
我正在努力使用具有外键的对象使用 Django 创建测试。测试 运行 时 运行 单独时没有错误,但是当 运行 全部时,我在 teardown
阶段得到 ForeignKeyViolation
(我不明白它是做什么的,但找不到任何相关信息)。
启动测试套件时,我创建了一堆这样的虚拟数据:
def setUp(self) -> None:
create_bulk_users(5)
create_categories()
create_tags()
create_technologies()
self.tags = Tag.objects.all()
self.categories = Category.objects.all()
self.technologies = Technology.objects.all()
我需要帮助解决的问题是:
teardown
阶段究竟做了什么?有详细的文档吗?- 我应该如何构建测试以避免
ForeignKeyViolation
问题?
拆解阶段具体做什么?有详细的文档吗?
我找不到任何关于它的详细文档。我所发现的只是阅读实际代码。拆卸会取消测试期间对数据库所做的任何操作。
The way TestCases work (broadly) is this:
1. SetUp method runs **before** every test
2. individual test method is run
3. TearDown method runs **after** every test
4. Test database is flushed
我应该如何构建测试以避免 ForeignKeyViolation 问题?
我的错误是我导入了一个用于创建虚拟数据的函数。 导入的模块是 运行ning 一个函数来填充一些主要数据。问题是在 setUp 方法中不是 re运行 each,因为在导入文件时它 运行 只是一个。
# imported module
# will delete all tags after running first test but
# the reference to the old tags will persist
tags = FakeTags.create_bulk();
def create_bulk_articles():
article = FakeArticle.create()
article.tags.add(tags)
我只是使用导入函数中的故障函数修复了它。
# imported module
# tags are created each time before running the tests
def create_bulk_articles():
tags = FakeTags.create_bulk();
article = FakeArticle.create()
article.tags.add(tags)