Reasons to be Cheerful - KISS principled testing

Jon Brookes

2025-01-06

Running my tests for a website again today, I find another reason to find peace calm and less than utter panic brought about by listening to current news and commentary on the last 25 years of this century and the potential for the years ahead.

My tests failed however, I was able to quickly see where they failed and step by step correct just 2 items as each test tailed and reported on such that all are running again now.

A simple principle in testing and coding more generally is not to repeat yourself which can be abbreviated to DRY deriving from dont repeat yourself.

The tests I had there were failing were due to a hurried implementation of a test to check for a footer being present on all web pages. This was in part hard coded and copy pasted into several functions in the tests.

I’m using the dusk test framework just now and that, like many others like it, has a setup method we can use to set up subsequent tests in the current suite.

public $copyRightString;
	  
protected function setUp(): void
{
	parent::setUp();
	$this->copyRightString = '© ' . date('Y') . '  ACME corp group and holdings. ';
}

then, in tests that need it can use this pre-computed value

$browser->visit('https://example.acme.com/products')
	->assertSee('MOST POPULAR')
	->assertSee($this->copyRightString);

So my test was ‘brittle’ but not in an entirely bad way. I had enough foresight at least to test for desired outcomes rather than for code implementation. The latter being a trap I and many fell into when the test all the things craze came upon us back in the day.

So by testing a top level ‘feature’ without which the objective of our app is purposeful we avoid over testing things that are not requiring such a level of attention and counter to this, we can re-factor our implementations safe in the knowledge that if we break critical functionality our tests will catch this before it goes out to our users.

This gives me for the moment at least, control over the things I have immediate responsibility and the encouragement to build upon a testing framework and testing approach to ongoing development of services.

I’ve heard it said that coding is a bit like gaming in that each triumph or success leads to a small release of endorphins in the body. A small reward subsequent to an achieved outcome. So too with testing if that testing goes alongside the development and maintenance of code that we own or manage has a more effective and fulfilling outcome than if you are coerced in some way to do something with little meaning or purpose.

Those far more intelligent than I have said that it matters not how much we may be concerned over or worried about things that are entirely out of our control as to effect an increase of a day of our life span. This is not to say we should not be concerned about anything. That would be to deny our humanity but it does mean that we have to focus on the things that we can do in the moment and ahead of this in order to play an active part in our own and others existence if we are to succeed as individuals.