How to run django unit-tests on production database?

前端 未结 5 1223
面向向阳花
面向向阳花 2020-11-29 00:56

i\'m starting the TDD development attitude and am writting unit-tests for my django application. I\'m aware of fixtures and know that\'s the way tests should be executed, bu

5条回答
  •  一向
    一向 (楼主)
    2020-11-29 01:38

    First, if you're running it on the production database, it isn't much of a "unit" test.

    It's a first-class batch job and needs to be treated like a first-class production batch job.

    You cam't to use the Django test command for looking at production data. It always creates an empty database which is populated from fixtures in the TestCase.

    You could make your production database processing a proper management command. This has the environment all properly configured so that your command can simply use the Django ORM to process your data.

    The alternative is to be sure that you configure your settings. Either use the DJANGO_SETTINGS_MODULE environment variable or use the settings.configure() function to create an environment.

    You can then import the models and do the processing you want to do against the production database.

    You can call it "test" if you want to, but you're looking at production data, so it's got to be treated like production application with respect to getting the settings file and using the proper ORM configuration.

提交回复
热议问题