My Authors
Read all threads
xpectr is a new R 📦 for generating testthat unit tests (expectations). Its goal is to systematize and ease the process of building good tests into packages.
It comes with multiple @rstudio addins and a set of commonly needed utilities. 1/n
#rstats #datascience #R @rstudiotips
I often end up writing the same set of tests again and again, which is honestly quite boring and time consuming. By generating these standard tests, I can spend my time thinking about additional, meaningful tests. Saves a lot of cognitive processing! 2/n
github.com/LudvigOlsen/xp…
The main functions are gxs_selection() and gxs_function(). The former has an @rstudio addin as well. There's also a useful dput() addin and an addin for wrapping long strings with paste0(). Let's go through these. 3/n
#rstats
gxs_selection() takes a string of code, runs it and creates tests for the result. The associated addin is what is shown in the video in tweet 1.
So basically, you highlight the name of a variable and press a key command, and the tests are generated. 4/n
#rstats
gxs_function() takes a function and a list of argument values to test. The first value supplied for an argument is considered the *valid baseline* value. For each argument, we create tests for each of the supplied values, where the other arguments have their baseline value. 5/n
gxs_function can be used for fuzz testing, although you have to supply the values to test yourself.
Going through the generated tests can quickly reveal unintended behaviors by your function. 6/n #rstats
As long as you keep the original call to gxs_function() (e.g. commented out), its easy to regenerate the tests when changing something in the function. Then you can use a diff tool to check that only the intended changes were made. 7/n #rstats
The dput addin applies dput() to the selected code and inserts the result. This is very useful when creating expectations manually. I've been wanting it for half a year, so I'm at least pretty excited myself :-)
8/n #rstats @rstudiotips
The wrapStringAddin() splits the string every n characters and wraps it with paste0(). This way it is still the same string, just not 5 screens wide! 9/n
#rstats @rstudio @rstudiotips
xpectr is on CRAN, so it can be used in packages. It also provides tools like smpl() for sampling with an internal seed, strip() and strip_msg() for removing punctuation from error messages (in case different platforms use different symbols), with more. 10/n
Call for feedback and collaboration:
I truly believe xpectr would be improved by a conversation on what tests to create in which cases. Different use cases lead to different tests. Please give feedback on GitHub or mail me! All thoughts are welcome! 11/n
Finally, let me just add that the goal is **not** to replace the active thinking process of finding meaningful tests for your functions, but to help systematize and ease that process. 12/n
#rstats
xpectr will only test the current behavior of your package, so always go through the generated tests. By easing the process though, it becomes easier to provide a good test coverage. 13/n
#rstats
Missing some Tweet in this thread? You can try to force a refresh.

Enjoying this thread?

Keep Current with Ludvig Olsen

Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

Twitter may remove this content at anytime, convert it as a PDF, save and print for later use!

Try unrolling a thread yourself!

how to unroll video

1) Follow Thread Reader App on Twitter so you can easily mention us!

2) Go to a Twitter thread (series of Tweets by the same owner) and mention us with a keyword "unroll" @threadreaderapp unroll

You can practice here first or read more on our help page!

Follow Us on Twitter!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just three indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3.00/month or $30.00/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!