A test in software development world is a piece of code written to verify that the code works as expected. An ideal test validates these 3 areas:

  1. Does the code solve the problem it was designed to solve?
  2. Does the code respond correctly to inputs? Both valid and invalid inputs
  3. Does the code perform optimally and reliably.

Image Credits: Chris Ried, marsner.com

What is TDD?

Test Driven Development (TDD) is a software development approach where the requirements are transformed into specific test cases and then code is written to satisfy these tests; we would typically write just enough code for the tests to pass; and then refactor the working code for better optimisation; nothing more.

Red-Green-Refactor: Tests are written first, these tests fail (Red) then we write code that makes the tests pass (Green) Then we Refactor the working code

Why should you care?

I am sure you’re already very familiar with manual testing, your manual workflow will be similar to this scenario; open up a page, fill the form with proper data and verify that the data is submitted(Happy path). Open up the form again but this time around supply bad data, could be text in a number field and verify that it fails successfully(Unhappy path). Imagine what happens as the app grows 😔

You see the need for automated testing?

Some of the benefits of TDD include:

  1. It is a required skill for most good jobs
  2. TDD helps with regressions so previously working functionality is not broken by new enhancements
  3. It helps verify the requirements developed
  4. It encourages a design first mentality for development
  5. It prevents over engineering as we’re only doing bare minimum
  6. Leads to fast delivery cycles
  7. It builds developer confidence with releases
  8. It encourages a customer centric design
  9. It ensures reduced maintenance cost
  10. It boosts confidence when refactoring and improving code — this is the biggest for me
  11. Writing tests also expose code smells, this one becomes valuable with time

There’s a post on different types of testing here

Testing approaches

  1. Black box testing: we assume we have no access about what happens on the inside but only validate the outputs from the system
  2. White box testing

Testing tools and frameworks

  1. SUnit frameworks eg JUnit for Java, NUnit for dotNet
  2. UI frameworks eg Selenium
  3. System frameworks eg Chaos monkey, Simian Army

Concepts

  1. Tests
  2. Test suites
  3. Before and after hooks
  4. Assert: used to tell the test the expected output
  5. Test runner — synchronous vs asynchronous

Strategies and techniques for testing

  1. Dependency Injection: in this strategy, the object is passed with the required dependency.
  2. Test doubles: test doubles are pretend objects. The two major ones are stubs and mocks; we also have fakes and spies.
  3. Best practices

Tests shouldn’t worry about implementation details

Limitations of TDD

  1. TDD is not a magic-wand; it doesn’t always prevent bugs just like bugs get past compilation. If you have a bug in the test code and implementation code, it can go undetected, hence, you need to understand the problem you are solving.
  2. TDD can seem slower at the beginning because you need to think about the interfaces, write the tests before implementing code to make these tests pass.
  3. One of the strongest arguments against TDD is that test need to be maintained when requirements change; you’ll need to change the tests and then implement code that makes this new tests pass.