← Back to context

Comment by arp242

12 days ago

I feel that trying to maintain "representative testing data" is generally not a good idea; set up the data you want/need in the test instead.

Just run PostgreSQL on your local machine, connect to that, setup a new schema for every test (fairly cheap-ish) inside a test database.

  def Test1:
    setupdb()
    obj1 = createObj1()
    obj2 = createObj2()
    have = doStuff(obj1, obj2)
    if have != want: ...

  def Test1:
    setupdb()
    obj = createObj1()
    have = doOtherStuff(obj1)
    if have != want: ...

Creating reasonably scoped reasonably contained "unit-y tests" like this means you will actually be able to understand what is going on. Too often have I seen people set up huge wads of "mock data" and then run all their tests on this. Then Test1 does something Test2 doesn't expect and you're screwed. Or worse: Test42 does something that screws Test185. Good luck with that. Or you introduce a regression somewhere and now you've got tons of data to understand.

Yeah the keys to make it all work are

1. It's easy to create the objects you need

2. Your creation functions are well tested so that the rest of your tests can rely on them.

If you have spotty coverage or just poorly defined creation semantics, or it's a bunch of calls to functions all over the place just to set up your test data, then this doesn't work.

But the solution typically isn't "write a bunch of JSON mock test data", it's to solve those problems.