I gave a little presentation at work the other day on using automated testing in my Laravel project using Codeception. Here’s a screencast. The audio is a little low, so there’s a basic transcript below the video. Enjoy.
[chrome: show feature request in jira]
so here’s a little example of writing an automated test to confirm a new feature.
[switch to jira]
there’s a request in my issue tracker to sort any “Other” option in dropdowns to
the bottom of the dropdown list.
[chrome: show “before” example in metrics]
see here how “other” gets lost in the list. we want it at the bottom. so let’s switch over to do that.
[phpstorm: implement feature]
ok, i can add the feature with a few lines of code. since this is a demo i already have the solution
i can just copy/paste in… save it. now back over to chrome
[chrome: show “after” example]
…and in the form and there’s “other” listed at the bottom. that’s great, perfect.
but we don’t want to have to manually test that this feature works every time i make
any new change to the code. i’d probably forget to test this little bit of
functionality if i had to do it manually. so it would be great if i could just push a button
and run automatic tests whenever i change any code in the application. that way we can make
sure we don’t break existing features like this nifty one we just implemented.
[phpstorm: add empty test and run showing failure]
so let’s do that. let’s create a test for this feature.
and let’s fail the test for now, to
remind us that we need to implement it. as a quick aside, i’m using a package called “codeception”
for my test suite.
[switch to codeception website]
codeception is an automated test framework written in PHP. it has different modules for various things like database testing, rest api testing, testing frameworks like laravel or symfony, and other stuff.
of course there’s plenty of other test frameworks you could use, like PHPUnit, Selenium,
Drupal’s built-in SimpleTest … but i like codeception for what i’m doing.
[phpstorm: run test]
anyway, let’s run our failing test … and of course it fails. so we need to make it not fail.
[phpstorm: test code]
luckily, i already have some test code here i’ll just paste in. this is pretty self-explanatory i think.
we tell it to log in, go to the particular page with the form on it, grab the dropdown options, and
assert that “other” is at the bottom.
[phpstorm: run test ]
so now let’s run the test … and hey success! the test passes. now anytime in the future
whenever i change code, i can run this automated test and get immediate assurance that i didn’t
break anything, at least not this feature.
for metrics, the api and supporting code, i’ve written almost 50 tests with 500 assertions. that’s
basically 500 manual tests i don’t have to run each time.
and so when i work on the code, i can press a button and run these suite of tests, and feel much
more confident that i haven’t broken anything.
so that’s a little intro to automated testing. hope it’s helpful. thanks.