7

当我通过键入运行测试用例时

python manage.py test myapp

测试用例完成后,测试数据库默认被 django test runner 删除。我不希望它被删除。

我可以使用任何数据库!

我想保留我的数据库,因为我想在创建的数据库中看到数据库中的错误。这样我就可以查明他们!

4

6 回答 6

16

test --keepdb您可以使用该选项防止测试数据库被破坏。

https://docs.djangoproject.com/en/dev/topics/testing/overview/#the-test-database

于 2016-10-03T01:36:31.377 回答
9

根据文档,您可以在运行测试后通过以下方式保留数据库:

$ python manage.py test -k

或者

$ python manage.py test --keepdb
于 2017-03-17T20:45:36.897 回答
9

虽然传递-kmanage.py test将保留测试数据库,但它仍会删除在测试用例中创建的记录。这是因为 Django 的TestCase类仍然会在每个测试用例之后重置您的数据库(django.test.TransactionTestCase将执行flush, 而django.test.TestCase将每个测试用例包装在事务中,并在测试用例完成时进行回滚)。

让 Django 保留测试数据的唯一真正解决方案是扩展TestCase类并覆盖重置数据库的代码。

However, if you do not have the time to do this, you can also make your test case pause execution before it finishes, giving you the time to inspect your database before it gets reset. There are several ways of achieving this, but, now, THIS IS A HACK, asking for user input in your Python code will make Python pause execution and wait for user input.

from django.test import TestCase


class MyTestCase(TestCase):
    def test_something_does_something(self):
        result = do_something_with_the_database()
        self.assertTrue(result)

        # Ask for `input` so execution will pause and wait for input.
        input(
            'Execution is paused and you can now inspect the database.\n'
            'Press return/enter key to continue:')

Alternatively, you can also use pdb's set_trace function, which will also make the execution pause and wait for input, and at the same time lets you debug the environment in that point of code execution.

Just make sure that you remove the input() (or pdb.set_trace()) call before you send your code to your automated build system or else it will wait for user input and time out.

于 2020-03-24T06:00:34.717 回答
4

To preserve whole database state after test execution (not only tables structure)

  1. Make sure your test class is based on django.test.SimpleTestCase (not TestCase or TransactionTestCase)
  2. Take one of your tests for which you want to preserve database state
  3. Add the following code to your test class to prevent database tables cleaning after the test execution
    def tearDown(self) -> None:
        pass

    @classmethod
    def tearDownClass(cls):
        pass
  1. Run the test with --keepdb parameter, like ./manage.py test app.test --keepdb - to prevent whole DB cleaning after test execution
  2. Wait for the test to finish
  3. Profit! Take snapshot/discover your test_database [do not forget that Django by default will add prefix test_ to your default database name]

Example of command for test test_copy

./manage.py test --noinput --keepdb api.tests.SomeTests.test_copy

class SomeTests(SimpleTestCase):
    allow_database_queries = True

    def setUp(self):
        super(SomeTests, self).setUp()
        self.huge_set_up_operations()

    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.huge_init_database()

    def tearDown(self):
        pass

    @classmethod
    def tearDownClass(cls):
        pass

    def test_copy(self):
        SubscriptionFactory()
于 2020-05-11T18:31:39.123 回答
0

For anyone in a Pytest environment I use the following pytest.ini for testing

[pytest]
DJANGO_SETTINGS_MODULE=myapp.settings.test
python_files = tests.py test_*.py *_tests.py
addopts =
    --ds=myapp.settings.test
    --reuse-db
    --nomigrations

note the "--resuse-db" command argument/addpots

于 2021-04-29T23:51:21.157 回答
-5

根据文档

无论测试通过还是失败,测试数据库都会在所有测试执行完毕后销毁。

虽然,固定装置可能对您的情况有所帮助。只需创建初始数据,您希望在测试开始时就在那里,作为纹理,并让测试加载它。

于 2011-01-06T14:37:05.447 回答