Write Unit Tests Faster with this Macro

Write Unit Tests Faster with this Macro

 

If you are reading this post, you probably know what a unit test is. If not, check out FunFunFunction’s youtube video on it. There are many blogs discussing both the pros and cons of it. Some would argue that writing unit tests is a waste of time. Others preach that its an important practice in “clean code” and scaling infrastructure. Regardless of what language or test framework you are talking about.

Personally, I view unit tests as a form of “notetaking”. It could be a huge pile of **** or highly praised cliff notes, depending on how well it’s written. It describes what a function should or should not do. That simple. People who read your /test folder can have a better grasp of what your code does. And the logic behind why you wrote it a certain way.

Yet, it has downsides. You might end up scraping code in your main repo. rendering that unit test invalid. Or refactor your code architecture so much that tests need a complete rewrite.

Also, unit testing takes times to write. It’s not unusual to have more lines of test code than your actual program. Especially for some languages and frameworks (looking at you javascript). There’s an opportunity cost, every test is lost development time.

Recently I adopted mocha+chai into my javascript development workflow. It’s flexible and easy to read.

BUT, it’s tedious to write. Take for instance this function

n9aJQut[1].png

Writing the unit test is 5 lines of code:

lHcqcs4[1].png

which takes way too long to write. You might opt to copy-paste your other test code snippets. But this adds another click, CTRL+C, CTRL+V, click, edit, repeat, etc. That takes far too long. Imagine having to write many unit tests. You waste a lot of time typing what is extraneous code. And you are prone to typo errors.

Wouldn’t it be great if there was a fast way to write unit tests? One that is cross-platform, regardless of your development environment? And that works both on macOS, and windows (not Linux though sorry)

Well, the good news is there is. There’s a macro program called phrase-express. Used in the medical industry to automate data entry of drug dosages for patients. Free as well. I’ve written some articles on it for my favorite notetaking app. It automates a lot of repetitive tasks. It is dead simple to use.

Here’s why programmers should use phrase-express:yPLNoBm[1].gif

In 7 seconds, I wrote an entire customized unit test case from scratch. No copy pasting needed. With 0 typo errors. I used assert.equal(a,b) as a generic test that I later edit.

I typed the following to invoke my macro

  1. di# —Short for “Describe It”. Function name I am testing.
  2. it# —Meaning the “it” part. What the function should be doing.

I use # to invoke all my macros. In total, I have about 100 macros. I’ve never triggered them on accident. You can set phrase-express macros to only run in certain programs.

I used this youtube video to learn how to set up phase-express. If you want to use my macro in the above gif, here are the settings you need to put on.

jesUZhg[1].png

it("{#input -head It -single}", function(){
assert.equal(a,b);

UlrKNjX[1].png

describe("{#input -head Describe -single}", function(){

 

Leave a Reply

Your email address will not be published.