Last week we left our hand-rolled JSON parser passing two sets of tests. The first set are those that we wrote to drive the development of the code - this is TDD after all. These check that we return the right result for progressively more complicated input JSON strings.
The second set of tests are the JSON Test Suite tests - designed to test that a parser accepts and rejects good and bad JSON respectively. Those tests are comprehensive, but don’t actually specify what output we should generate for each input.
Now we would like to check that our parser returns the correct results for any input, not just those that we can be bothered to type in. Luckily we’re computer programmers, - we can substitute software for typing. So today we’re going to look at programatically generating test cases.
This is episode 9 in a series on writing a simple JSON parser in Kotlin. You can see the playlist here - • TDD JSON Parser and the code on GitHub https://github.com/dmcg/json
In this episode
00:00:54 Review of our current tests
00:02:40 We would like more tests where we know the expected results
00:03:06 Create JacksonTests
00:04:06 Check that we can read a string that Jackson has written
00:05:01 Extend to a list of different things
00:06:00 Discover that we are reading ints as BigDecimals
00:06:26 Write a unit test for the desired behaviour
00:07:33 A little light refactoring of a when expression
00:08:18 Add a special case for integers
00:08:34 AI Assistant for the (regex) win
00:09:37 But it wasn't perfect
00:10:33 So ask it to fix things
00:11:10 Test longer ints
00:12:04 Change to parsing a long
00:13:10 Add boundary condition tests
00:14:20 Now BigInteger
00:17:06 Deciding on ints in exponential notation
00:18:14 When is 100 not equal to 100
00:18:32 Unit tests are complete, return to Jackson
00:19:04 Oh but 0 is a number
00:20:23 Checkin
00:21:18 Expand our Jackson tests with lists of strings
00:22:28 and numbers
00:24:05 Drive home and then look at combinations of things
00:26:47 Check map values
00:29:57 Test maps in a list
00:31:13 Test lists in a map
00:31:47 Maps in a map
00:34:09 Check key names
00:36:10 How about random combinations?
00:36:34 Generating random lists
00:39:25 Random maps are easy
00:39:49 Now make it random all the way down
00:42:10 We can go too deep it seems
00:43:20 Record the random seed so that we can reproduce any failures
00:46:13 Commit
00:46:21 Review
00:46:50 Next week
You should probably watch this if it interests you - Property-based testing with jqwik and Kotlin
• Property-based testing with jqwik and...
If you like this, you’ll probably like my book Java to Kotlin, A Refactoring Guidebook (http://java-to-kotlin.dev). It's about far more than just the syntax differences between the languages - it shows how to upgrade your thinking to a more functional style.
Смотрите видео Expert Testing - Generating Test Cases for our JSON Parser онлайн без регистрации, длительностью 16 минут 40 секунд в хорошем качестве. Это видео добавил пользователь Pairing with Duncan 10 Ноябрь 2023, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 471 раз и оно понравилось 19 людям.