Skip to content

Instantly share code, notes, and snippets.

@weaverryan
Created August 4, 2016 21:04
Show Gist options
  • Save weaverryan/bd81ea6a8029863ff58e2bb4cbc1a6d7 to your computer and use it in GitHub Desktop.
Save weaverryan/bd81ea6a8029863ff58e2bb4cbc1a6d7 to your computer and use it in GitHub Desktop.
Testing Questions with my Answers (fwtw)

From a user on KnpUniversity.com:

How do you manage testing form validation situations?

For form validation situations, I typically only write 2 scenarios (or sometimes just 1): one that checks that if I submit invalid data, that I see a validation error (I don’t, however assert that all of the validation errors are there – just that the form has some validation errors). Second, I write a scenario that checks a successful form submit.

For me, creating many scenarios to submit a form and check for the different validation errors is over-kill. 99% of the time, adding validation – in Symfony for example – is just one line of code. If we write a scenario for each, we now have one scenario just to test whether or not one line of code is present. For this, I check the validation errors manually – just to make sure I didn’t forget them. I’m not worried about a regression, because it’s highly unlikely that we’ll accidentally remove a line of validation and introduce a bug.

However, if there is some really critical validation situation that must never be forgotten, then write a scenario for it so that you can feel confident :). Or, if you wrote a complex custom validation situation, you can also test that.

How do functional and unit tests compliment each other?

I mostly use Behat for tests – I don’t use a lot of unit testing. This varies person-by-person, but for me, unit testing is useful when I have an individual function that is so complex, that I feel the need to write a unit test to make sure I’ve programmed it correctly. Even in these cases, however, you could probably write a functional tests instead of a unit test – to perform some action on your site that indirectly tests all the input and output of that function. How do I decide which to do? Mostly, which is easier? If there is truly a single, complex function – adding a unit test is easy and effective. But if there are multiple, simple pieces interacting, which taken as a group become complex, that’s great for a functional test.

Cheers!

@greg0ire
Copy link

greg0ire commented Aug 5, 2016

Second, I write a scenario that checks a successful form submit.

I found a very related question, so if you or anyone know how to use transactions to rollback to the previous state with behat, I'd be very happy to give 500 reputation for that.

Otherwise, your "trolling bait" makes a lot of sense, and I'd say I choose to write a unit test if I can easily get value out of it, which happens when the code is not coupled to some external API like Doctrine (which can happen a lot in RAD applications). In that case, I try to do it even if the code is quite simple, b/c it forces me to design the object's API first, making my natural lazyness optimise for "right now", which is "writing the unit test", which should force me to design as simple an API as possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment