Code Testing

I’m now really trying to push the development mindset of Test Driven Development (TDD) and Behavioural Driven Development (BDD) into all of my projects.  It’s a great way to keep your code clean and follow best practices while development, which in the long term will pay off when making updates and further additions to your codebase.  Ensuring that you have a series of tests that you can run through to automatically check if all aspects of your app are functional after a development cycle is not only a time saver but gives you the confidence that pushing your code to the production environment won’t cause any issues.  (In some case you may be using something like Jenkins as a continuous integration solution which won’t push unless the tests come back all positive).

The essence of TDD and BDD is that you write your tests in conjunction to writing your code.  Say you need a function that returns the sum of two integers.  You would first write the test for that function and the expected result from the supplied parameters, then write the function.  This way everything you write will have a test already built for it, building up your test-base as you go on.

BDD is pretty awesome as you can satisfy site requirements of real user journeys and user activity with tests.  Unit Tests will test your code which is great but your code could be all fine but the actually objectives the user wants to perform on your site may be completely broken.  This is where BDD comes in so that it will give you clarification that the site is operational and satisfies all requirements that it’s supposed to perform.  For example: Given I am on “/search”, When I fill in “search” with “Fast food restaurants”, And I press “searchButton”, Then I should see “Results for Fast food restaurants”.

Currently I’m using Behat and Mink for BBD with Laravel 5 and the standard PHPUnit testing built into Laravel for my TDD.

While these two methods are great and I really recommend performing them while developing, we live and work in the real world, with time and delivery pressures.  Sometimes clients require something up and running within short periods and writing tests along with your application will take a longer amount of time then you actually have.

When this situation arrises I’ve found that if you always keep in mind the question “Can I test this piece of code” it encourages you to always follow best practices and keep your codebase clean.  So when I get some downtime while waiting for client reviews I can then write my test cases.

Obviously this isn’t the desired way of working, but the end result is you still have a series of tests once development is complete.  Also, in the future you can use these tests to ensure everything is working as desired if you make any changes to your code.