question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for snapshot testing

See original GitHub issue

Consider the following test case:

Given a SQL query the system should produce the following results (in any order). The case is repeated for various SQL queries.

Of course I can use

sql = "..."
assertEquals("results for ${sql}", """
   expected line 1
   expected line 2
""",  execute(sql).sorted())
sql = "..."
assertEquals("results for ${sql}", """
   expected line 1
   expected line 2
""",  execute(sql).sorted())
sql = "..."
assertEquals("results for ${sql}", """
   expected line 1
   expected line 2
""",  execute(sql).sorted())

however if the implementation changes, then it would be very hard to update all the test to new expected values. It would basically force the developer to manually go and update each and every assert statement.

I wonder if kotlintest could integrate something like “self-reproducing” test cases, so it creates “new test code” as one of the results of the test execution.

That is:

  1. It performs the assertions as usual
  2. It writes “target/tmp/…kt” file with new source code. If developer agrees that all the “failures” are expected, he can just grab the new file and copy it over the existing one

Here’s an example of such a test: https://github.com/apache/calcite/blob/3c6b5ec759caadabb67f09d7a4963cc7d9386d0c/core/src/test/resources/sql/join.iq

For instance, !plan at line 62 asserts that execution plan of a previous query matches the plan listed at lines 39…61. The result of .iq execution is a brand new .iq file with “expected values” set to the current actuals.

In other words, if a test passes, then .iq is a quine.

.iq stands for https://github.com/julianhyde/quidem there.

I wonder if that test idiom can be incorporated and expressed in Kotlin.

Pros are as follows:

  1. The test is somewhat easy to read
  2. The test is easy to write: just start with EMPTY assertion, and test output would produce the text of the test WITH expected value for you
  3. It is trivial to maintain: one reviews the diff, then copies new file over the old one
  4. It is fun

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
sksamuelcommented, Nov 27, 2021

Will look to include this for 5.1

0reactions
sidgatecommented, Jul 4, 2022

Approval tests works well for API testing, I can just have the JSON verified. Now I cannot use kotest for such scenarios.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Snapshot Testing - Jest
Snapshot tests are a very useful tool whenever you want to make sure your UI does not change unexpectedly.
Read more >
Snapshot Testing: Benefits and Drawbacks - SitePen
We explore some of the advantages and disadvantages of using snapshot testing as well as some tools and resources for using it successfully....
Read more >
Creating snapshots in Jest for testing React applications
Using Jest snapshots will help you ensure that your UI changes are deterministic and that you are aware when changes are made. Using...
Read more >
What is a snapshot test? - Lara Schenck
Snapshot testing is a common practice in the JavaScript ecosystem, for example, to check that a React component outputs the correct JSX or...
Read more >
Snapshot Testing: Example and Its Benefits - ORIL
One of the disadvantages of snapshot testing is that there is no test-driven support. But it doesn't make a big difference since it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found