Expected, actual, and super fast assertion creation
If you follow this blog[1] you know that one thing I enjoy is improving my workflow inside my IDE. The feeling of productivity of being able to code something in seconds instead of minutes brings me joy and lets me keep my focus on other things. I recently had a new improvements idea for test creation[2].
I think that a great idea to use in tests is to prefix variables with a special role and have a convention for it. My personal preference is to prefix the variable containing the expected result with ‘expected’ and the one containing the value you want to check with ‘actual’.
I decided to create two custom postfix templates that, for some value, create a variable with the wanted prefix and trigger auto-completion to generate the end of the variable name.
The flow goes like this:
- Create a value:
new Pony(‘Eole’)
- Type
.ev
- Get
$expected<cursor> = new Pony(‘Eole’);
- Let the autocompletion guide you. Here it will typically suggest something interesting based on the value, like
Pony
in our example. If you’re dissatisfied with the suggestion, type something else. Tab ⇥
- Done.
See it in action here:
I let you imagine how it works with the av
postfix template to get an actual
prefixed variable.
It took me a few minutes to get a variable name in camelCase because the auto-completion returns a capitalized name. Thankfully, you can use GroovyScript inside templates, which makes it easy to lower the generated string.
You can avoid the work of recreating those two postfix templates by copying the following snippet in the plugin configuration file.
.ev : Create an expected variable
ANY -> \$expected$var:capitalize(groovyScript("if(_1) { _1.substring(1) } else { _1 }", phpSuggestVariableName()))$ = $expr$;$END$
.av : Create an actual variable
ANY -> \$actual$var:capitalize(groovyScript("if(_1) { _1.substring(1) } else { _1 }", phpSuggestVariableName()))$ = $expr$;$END$
But that’s just the first part of the improvement.
Now that I had a convenient way to generate my expected and actual value, it unlocked a new idea: let’s generate assertions with the variable already there.
For this, I created a new live template, asseq
which adds the code for an equality assertion, $this->assertEquals($expected, $actual);
and triggers autocomplete after the two prefilled parameters. Getting an assert equal in a test is now as easy as typing asseq
, Tab ⇥
, Tab ⇥
.
See it here how fast it goes :
And here is the live template snippet to add to your configuration if you want to use that trick to.
<template name="asseq" value="$$this->assertEquals($$expected$VAR$, $$actual$VAR2$);" description="" toReformat="true" toShortenFQNames="true">
<variable name="VAR" expression="complete()" defaultValue="" alwaysStopAt="true" />
<variable name="VAR2" expression="complete()" defaultValue="" alwaysStopAt="true" />
<context>
<option name="PHP Statement" value="true" />
</context>
</template>
As a bonus, here is the snippet for a second live template that allows you to select what assertion you want because you sometimes need something other than an equality assertion. It does the same thing as the previous one but adds an extra stop and autocomplete step to select the assertion.
<template name="assert" value="$$this->assert$ASSERT$$END$($$expected$VAR$, $$actual$VAR2$);" description="" toReformat="true" toShortenFQNames="true">
<variable name="VAR" expression="complete()" defaultValue="" alwaysStopAt="true" />
<variable name="VAR2" expression="complete()" defaultValue="" alwaysStopAt="true" />
<variable name="ASSERT" expression="complete()" defaultValue="" alwaysStopAt="true" />
<context>
<option name="PHP Statement" value="true" />
</context>
</template>
I hope this blog post will give you other ideas on how to use live templates and postfix templates to improve you workflow and bring more joy to your work life.
As you can see, I enjoy teaching about testing or help dev teams being more productive and working in a safer way. If you feel like your team could enjoy imrpving the way it works let's have a chat and see what we can do together.
- Improve your automated testing : You will learn how to fix your tests and make them pass from things that slow you down to things that save you time. This is a self-paced video course in French.
- Helping your teams: I help software teams deliver better software sooner. We'll work on technical issues with code, test or architecture, or the process and organization depending on your needs. Book a free call where we'll discuss how things are going on your side and how I can help you.
- Deliver a talk in your organization: I have a few talks that I enjoy presenting, and I can share with your organization(meetup, conference, company, BBL). If you feel that we could work on a new topic together, let's discuss that.