Unit testing is something that you simply can’t avoid, because it’s the very thing that helps to verify that your code is in fact good, and works as expected. You might think you’re avoiding unit tests, but most of the modern IDEs these days have implemented automatic unit tests already.
So, in case you’re new to unit tests, to put it simply: it’s a way of making sure that the functions you’re creating, are working properly, and not throwing errors. This is particularly helpful, because you want to work in environment where workflow is not a problem
I strongly believe that unit testing is not just something that happens, or can happens, it’s a natural part of the programmers life, and should be looked at as something that adds flavor to the whole experience. I recommend watching the talk below, it’s by Miško Hevery, and he gives us a great lecture on testing and what it is.
You can write your own unite tests, if you feel adventurous and up to par with the skills required, but for most people; a framework is usually enough to take care of (tackle) most of the problems.
The whole point of tests is to run them a lot. Particularly in larger teams where dozens of developers are working on a common code base, continuous unit testing is important. You can set up tests to run every few hours or you can run them on each check-in of the code or just once a day (typically overnight). Decide which method is the most appropriate for your project and make the tests run automatically and continuously. [RW]
Make it a habit to do unite tests whenever possible, it will pay great dividends in the future, and it’s probably one of he most efficient ways of saving time.
1. Cucumber: Behaviour-driven Development Tool
Just to make it perfectly clear, the above snapshot is taken from this homepage, which is the foundation of the Cucumber.js project; so don’t get confused, the actual Cucumber.js is still a work in progress, but it can be used and is encouraged to be used, the quicker we can iron out bugs and other little things, the quicker a stable version can be released.
It has gained enough attention in the recent months, to be included in the ever popular WebStorm 8 release, so there is a lot of potential. You’ll need to introduce yourself to Gherkin, in order to use this unit testing framework.
2. Jasmine: Behavior-driven Testing Framework
I realized that there was a podcast around Jasmine not so long ago, and after some researching I was able to find the link to it, watch it here and you’ll learn a few things that Jasmine is good at, and what it can do for your development workflow.
Jasmine is highly recommended to both beginners, and intermediate programmers; it’s easy to get going, and doesn’t require an enormous learning curve to get the most basics things done. (like testing)
It does live to up its name of being a very feature-rich framework, and has multiple interesting features, such as:
- test-specific timeouts
- highlights slow tests
- use any assertion library you want
- optionally run tests that match a regexp
- simple async support, including promises
It’s built by the team that is behind the jQuery foundation, so there is definitely a sense of experience and trustworthiness that comes with using QUnit, it’s also one of the most friendly frameworks from this list, as far as doing tests quickly goes.
QUnit is especially useful for regression testing: Whenever a bug is reported, write a test that asserts the existence of that particular bug. Then fix it and commit both. Every time you work on the code again, run the tests. If the bug comes up again – a regression – you’ll spot it immediately and know how to fix it, because you know what code you just changed. [GH]
You do not need jQuery in order to work with QUnit, all you really need is the framework itself and a browser.
I totally get that there is more to this than just some text, explaining and giving a brief introduction what the each framework has to offer, but the best way to go about these is by picking one, and getting to work.
The last video that I linked for the QUnit framework, perfectly showcases how most of these frameworks operate, and it’s the same code writing that you’d usually expect, only less painful; as it’s all presented to you in a framework, with pre-determined functions and operations.
Watch the intro video that I showed at the beginning of the post, it will go a long way when it comes to grasping unit testing, and will help to better understand just how important it is.