Thursday, December 10, 2020

Why We Write Unit Tests

When I left off in early November, I had largely gotten my development environment up and running. That meant it was time to start delving back into the code. The first thing I'm going to write for Robot Fortress is an authorization and user data service. After all, this is going to be an online multiplayer game and to do that I'm going to need to expose a bunch of service endpoints to the brutal chaos that is the modern internet.

I've got quite a lot of experience writing these kinds of services using a Linux/Nginx/MySQL/PHP stack, so it's also a nice place to start and get my head properly back into 'coding space.' Writing the client in Unity is going to be the real stretch (though I do know C#) so I want to start with something more familiar.

As it happens, I have a php library that contains a number of useful classes that will make writing this service a bit easier. However, it is out of date in the following ways:

  • The library was last updated to be compatible with 7.1 but the current installed version of PHP on my local environment is PHP 7.4.11 
  • There have been two additional patch updates to PHP 7.4.13 but Amazon Extras doesn't appear to have those yet so I can't upgrade my dev environment or my production server.
  • At last update, the library's unit tests were written again PHP Unit 7.3.5 but PHPUnit is currently on version 9. As it stands, the version of PHPUnit installed on my local machine is 7.5.20.
  • All of the composer packages that the library is built on top of were up to date for PHP 7.1, which means they're all out of date as well.

So here's what I did:

  • Ran the unit tests and saw what failed (quite a lot) either because of the update of php from 7.1 to 7.4.11 or the update of PHPUnit from 7.3.5 to 7.5.20.
  • Updated the unit tests and the code until everything passed.
  • Individually updated each of the composer package.
  • After each composer package update, re-run the unit tests, noting what failed and then fixing either the code or the test as appropriate.

In the end, 854 tests are passing and 3 are currently skipped. The 3 that are skipped are from tests that appear to be failing due to changes in how objects are mocked in PHPUnit. Since I'm going to have update the entire library to be compatible with a 2 major version upgrade to PHPUnit, I decided to defer fixing those tests until that time comes.

I can't even imagine how difficult this would have been if the library had not come in with a high level of unit test coverage.

THIS IS WHY WE UNIT TEST.

THIS IS WHY WE USE DEPENDENCY INJECTION.


No comments:

Post a Comment