Why not always using snapshot tests instead of conventional unit tests?

Hi, I really like the idea of snapshot tests.

It is weird for me to think about all kinds of expect_* for my functions, so I wonder why not always using snapshot tests for every single function? What are the drawbacks of snapshots tests?

I think there are a couple of reasons not to use snapshot tests:

  • It is often hard to do test-driven development with snapshot tests.
  • Some objects are hard to print, but easy (or easier) to compare.
  • Storing lots of long printouts might need a lot of disk space.
  • Print methods change (e.g. tibbles are printed differently recently), and that breaks all you snapshot tests.
  • Personal preference.

There are possibly more.

1 Like

Thanks! It is really useful!

I feel overwhelmed by all the available tools for testing, I wonder which one do you use the most from this list:

https://indrajeetpatil.github.io/awesome-r-pkgtools/#general

It feels like {patrick} and {exampletestr} might help me with the DRY principle for tests and documentation.

FWIW we (at the tidyverse team) write a lot of tests, and our experience shows that you can go pretty far just by using testthat.

This is not to say that the other, complementary tools are not useful, but rather that you don't really need to worry about them just yet. It is good to know about them, so if you find something cumbersome in testthat, or you need more specialized tests (comparing images, testing HTTP, etc.) then you can check them out again.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.