I'm developing a ML/AI solution and I'm looking to write some unit tests.

I end up with large objects with a lot of data. These come from a data source which is built at runtime. The data created as a composition of information retrieved directly from an API - I have mocked that response into a simpler result, but in order for any test to be meaningful, the full dataset needs to be included.

How do I mock the resultant data source that gets hydrated? Do I have to create a class with static data? That doesn't seem right- it'd be huge, and I'd have to do a lot of it manually. I have sample data, but I'd still have to hydrate it at runtime.

What's a better way to do this?

  • is it over an http api? There's proxy servers for this kind of testing that can capture real api responses and then replay them later transparently
    – Daenyth
    Jul 18, 2017 at 13:31
  • It is, I just captured a response set for a particular time and stored it in a file, which is read at runtime. I don't want my tests to be dependent on that, though.
    – Athena
    Jul 18, 2017 at 13:32
  • You may be over-thinking the problem. If you have a dataset, even if it is stored in a file, that you use in your testing process, it is part of your assets. "Hydrating" the dataset is part of the mocking process. So, check the dataset file into your source code management system, and resign yourself to having huge SCM entries. It can't be helped. Jul 18, 2017 at 13:38
  • 2
    Why do you need the large dataset for unit test? If your test is ensuring how quickly code executes, you don't have a unit test. You have a performance test. Jul 18, 2017 at 16:32

1 Answer 1


If one reads a set of unit test rules it says that:

  • A unit test is not a unit test if it touches the file system

So, option 1 is code/class files that have this data. However, these will be large and cumbersome.

In this case, it is OK to bend the rules slightly. Simply create a directory in your source code that has the test data (files). Ensure the file(s) get copied to the output directory during the build and run the tests. This will be easier to manage.

As a post test step, one can copy the entire output to a "packaging" area and omit the test files and/or directory. This way they are not deployed.

  • As a side note, one may want to mark these tests as "boundary crossing" or some other category since they are not a pure unit test.
    – Jon Raynor
    Jul 18, 2017 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.