Approvals New Features - Date Scrubber and Custom DiffTool
Have you tried out Approval Testing yet? if no, why not? It's a great way to tame Legacy Code.
![Approvals New Features - Date Scrubber and Custom DiffTool](/content/images/size/w2000/2024/04/image_2024-04-17_112413946.png)
I found some freetime recently, so I added a few features to Approval Testing. I thought I'd just point them out here.
Date Scrubber
The best way to illustrate using the Date Scrubber is with an example. Let's start with something simple. Here is a test for some simple code that logs a few values (using the HSE Logger) and then checks the output file.
![](https://blog.sasworkshops.com/content/images/2024/04/image_2024-04-17_113524808.png)
When I run this VI, Beyond Compare pops up with the following:
![](https://blog.sasworkshops.com/content/images/2024/04/image-6.png)
Notice that each entry includes a timestamp. The problem with timestamps is that often they vary depending on when the code is run. That is problematic. If you want proof, just approve the results and rerun the test.
![](https://blog.sasworkshops.com/content/images/2024/04/image-7.png)
We want every run of our test to produce the same data. One solution to that is to use some sort of Test Double to mock the time. That can work and it requires adding an extra dependency and rewriting your code to use the Mock. With Approvals, there is an easier way.
NOTE: The logger was the easiest way to generate some test data that contained a bunch of timestamps. What I am going to show you works with any data that contains timestamps regardless of where it comes from.
So let's use a Date Scrubber. The Add Date Scrubber entry in the Approval Tests>>Options palette drops the following:
![](https://blog.sasworkshops.com/content/images/2024/04/image-1.png)
So then you simply copy one of the dates from the received file and paste it in the text constant that says Sample Date. Then wire up the Options. When you are done it should look like this:
![](https://blog.sasworkshops.com/content/images/2024/04/image-2.png)
Now when you run the approval test, the output should look like this:
![](https://blog.sasworkshops.com/content/images/2024/04/image-8.png)
Note that the dates in the received file have been replaced with placeholders with an incrementing counter. That is the Scrubber in action. In this particular case no dates repeat. If a date did repeat both instances would have the same identifier. This test tactic works in this case because we don't care about the exact timestamp just that it increments.
At this point, we can approve the results by copying the contents of the approved file into the received file. Rerun the test and it should now pass.
![](https://blog.sasworkshops.com/content/images/2024/04/image-9.png)
The Scrubber should recognize most common date/time formats. If it doesn't appear to recognize your format, check the Found? boolean. If it's false then you'll have to add your own custom regex. If you open up the Find Scrubber for Date VI it should be obvious where to add it. If you do, be sure to add a test case to Test Scrubber Found For All Date Types and submit a pull request - https://github.com/approvals/ApprovalTests.LabVIEW - so that everyone else can benefit.
Custom DiffTool
You may have noticed in the previous screenshots that I am using Beyond Compare. It is a really nice difftool, however it is paid. Winmerge is a free alternative. Approvals supports Beyond Compare and Winmerge out of the box without any configuration (you just have to make sure they are on your path). However some people would prefer to use a different difftol like VSCode, PyCharm, Notepad++ etc. Now you can!
Simply add a LV_APPROVALS_CUSTOM_DIFFTOOL
environment variable (only works on Windows). Set the variable to the command used to call the difftool. Use "%RECEIVED%"
and "%APPROVED%"
to indicate where the approved and received file paths to go. Here is an example for NotePad++.
![](https://blog.sasworkshops.com/content/images/2024/04/image-4.png)
Need Help With Legacy Code
Approvals is a great tool for Legacy Code. It is just one of many tools and simply having access to tools doesn't guarantee success. If you'd like to learn about more tools for dealing with Legacy Code and learn how to use them successfully, please reach out. We'd be happy to help.