Wednesday, February 22, 2017

Re: Initial data, tests and migrations

On Wed, 2017-02-22 at 14:22 +0000, 'Tom Evans' via Django users wrote:
On Wed, Feb 22, 2017 at 12:47 AM, Melvyn Sopacua <m.r.sopacua@gmail.com> wrote:  
On Tuesday 21 February 2017 19:00:42 'Tom Evans' via Django users wrote:
Previously, these instances were loaded from a JSON fixtures file,
And you can still do that.
which used to be the recommended way. For our own tests, we simply
load these fixtures in the setUp portion of the test; obviously we
can't go around modifying tests in third party libraries.
I tried taking them out of the fixtures, and adding them instead in a
data migration, but this also had the same effect - the instances were
there for the first test ran, and then missing for all the subsequent
ones.
setUp is run between test methods of a test case. setupTestData is a class method on the testcase run once per test case.
What is the "correct" way of ensuring that these instances exist in
the test database before any test is run?
All explained in the docs. Either redeclare the same fixtures for different test cases or reorganize your testcases to share the same fixture(s).
I think you have misunderstood: 1) The test cases belong to a library and cannot be modified 2) The initial data is populated from a data migration and not from a fixture, as that is not recommended [1] Data is placed in to the test database using migrations, but after the first test has been run the data is flushed out again, causing the second test to fail. I would like the all the tests to pass: * I don't want to put the same data into fixtures as well as migrations (DRY) * I don't want to modify the 3rd party library. How? Cheers Tom [1] https://docs.djangoproject.com/en/1.10/howto/initial-data/#providing-initial-data-with-migrations

The URL you refer to mentions about loading data for apps. Doesn't mention anything in regards to tests. Why are fixtures bad for tests? In fact, right above the "Providing initial data with migrations" section is a "See also" box that says "Fixtures are also used by the testing framework to help set up a consistent test environment". It would seem to me that the docs are telling you to use fixtures for tests and use migrations for real data.

--   Adam (adam@csh.rit.edu)    

No comments:

Post a Comment