unit-testing2I was recently asked by a good friend of mine whether or not Unit Testing are a must, or should he just stick to End to End Testing.

End to End Testing has some solid Pros:

  • You can let Testers/QA define most of them, they might even be able to build them completely, thus developers spend less time on them
  • The more your website gets full of features and complex - the higher the chance your QA might miss a button/feature to test. E2E Testing prevents this from happening
  • It's the closest thing to the actual user experience

So if they're so perfect and easy on the developers, why should we add Unit Testing as well?

The answer lies in the complexity of the project.
The more your project gets complex, the harder it's going to be to find out why the tests are failing.

Consider this example:
Say you're using Facebook Connect to login your user, once the user is logged in - he's redirected to his Inbox.

Within this test you check if the user already exists, you log them in (via Facebook Connect API) if so, otherwise you add the user to the database.
Finally, you refresh the page and show the user's inbox (or something of sorts).

The End to End test might attempt to login, realize that it's not seeing the inbox and output an error.

So Where's the problem?
It could happen on any one of these stages:

  • Connecting to Facebook Connect API
  • Checking if the user exists or not
  • Writing a new user to the database
  • Redirecting to Inbox
  • Rendering the Inbox page

This is a lot of things to check, and we're supposed to build tests to avoid such nightmares.

This is where Unit Testing comes into play.
With a unit test on every major function, you'll be able to easily point to where the process went wrong, thus save a LOT of time.

Some words on Testing Libraries
The testing world is complex and confusing at times, here are some short descriptions for different testing tools:

  • Unit Testing Frameworks (JavaScript) - Mocha/Jasmine, I personally choose Mocha most of the time, but both are quite similar
  • Behavior Driven Development (BDD) - Cucumber, very popular with Ruby developers, I've yet to work with it on production level projects, but I can see the advantages of having human readable tests
  • Test Runners - Karma/Velocity, these help you run multiple tests on real browsers, Karma is widely used for NodeJS and Velocity is the official Meteor test runner
  • Browser Automation - Selenium, this is definitely more of the End to End kind of testing, the highest level there is - automating the browser to test your application. Ideally it should be used more by QAs than devs.

More information about Testing: