You'll Never Believe What This Man Has to Say About Unit Testing!

1.0. Why you want to test
1.5. Tools are flipping great use them
2.0. How you will test

I've only recently been realizing the potential of test cases. For the longest time I've hated them. Like learning your country's second official language. But with time you begin to realize how much these things improve the quality of your life. I'm a one man jack of all trades-master of none kind of guy. The reality is, guys like me can't do it all in a reasonable time frame. There is just physically too much to do. Sure cutting corners here and there is alright, but when it gets to the point where it's just being a time sink, you should step back and re-evaluate how you're doing things.

I spend hours testing and debugging. I blame my lack and mix of knowledge between programming languages and frameworks. Missing semi-colons, parenthesis's, and even class notation just eat my time. Some languages use these, some don't - that's just life. So we have to deal with life. We improve our lives by inventing new tools. That's what it means to be human. So why would you - no - anyone ever disregard a tool created by someone who clearly had an issue and it helped? Surprisingly this happens way too often in our industry. Of course there are crap tools. There are also tools that companies pump millions of dollars into. Millions of dollars isn't nothing. People complain about too many frameworks, too many options. To me this is FOSS succeeding to a great degree.

- End of Rant -

CasperJS is a testing framework. Wait don't go! Please don't be scared of testing. It is a tool. Use it! I'll give you a solid reason to use testing:

A successful test gives you a promise that at least one thing is working in your application.

Isn't that fantastic? When you make changes to your application, the test (case) will say, "No, this isn't right! You messed this particular thing up!", and then you can look at what you tested and go "ah yes! I forgot this thing affect that thing!", and fix the issue. 

You just saved yourself 2 hours in the future.

Multiply that by 1 bug for every 100 lines and you're looking at a whole lot of anime.

So back to CasperJS. I won't talk about how CasperJS works. We don't care how it works. If people do care they can just use a search engine and fill that hole. We don't care though. We are real people with real work to get done.

Your first step to installing CasperJS is to visit this web page, where it will always have the most updated installation information. More updated than any blog. Even this one.

The second step to achieving enlightenment is to install a bunch of web-related applications. I'm purposely not going to tell you what they are. We are focusing on testing.

On Debian Sid you can install what you need with: sudo apt-get install nodejs phantomjs

Excellent, your test environment is now all setup. Amazing. Only two steps to go.

The third step is to write your test cases. Lets write a test case that grabs the top links from...Reddit! If any don't contain the word "cat", the test case will fail.

The final step is to run your test. Easy peasy. Open a terminal/command prompt/console/beverage and type this in: casperjs test reddit_cat_test.js

Your results will vary! Because there actually may be links on Reddit with cats! Here's what I got:

Your test case will probably fail too. But that's ok. Your test case kept its promise - it promises there are no cats on Reddit's front page.

And that's it! You can extend this to any web-related testing. I've never felt the urge to test different testing frameworks before. Maybe that's what it's like to mature as a software developer.


Popular Posts