Test style: AAA or GWT?

Many years ago, after really getting into writing tests, I fell into the same trap newcomers do: my test code was very messy! Eventually, I heard of the AAA-style of organizing test code. AAA stands for Arrange-Act-Assert. There are several posts out there that show it. Here’s a link to one, in case you don’t know what this is all about.

After years of following that format, I learned about BDD (Behavior-Driven Development), and that’s where I learned about GWT (Given-When-Then).

Several years ago, I stopped thinking about AAA when writing any kind of test and got convinced that I should always think in terms of GWT. Why? Because AAA is too technical, while GWT works well both for technical people as well as for business or QA folks. Now, why’s that important?

For me, when I want to get feedback by having my code reviewed, if I stick with the GWT mindset, I can get pretty much anybody involved with the project to help me review my code. That means I can easily work with another developer, business analyst, or QA guy.

So, when I’m about to write tests, instead of thinking how I’m going to…

  • Arrange my test, I think what I’m Given
  • Act in my test, I think When something happens…
  • Assert the results, I think if that something happens Then this is what I expect as the outcome.

In both styles, the structure of the test is pretty much the same. However, this shift in how I approach coding makes me think more from a business-oriented point of view, and this mindset opens up possibilities regarding the number of people I can collaborate with in order to build a better product.

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: