loading words...

Jan 24, 2019 20:40:19

Code duplication in unit tests

by @valentino | 212 words | 67🔥 | 365💌

Valentino Urbano

Current day streak: 67🔥
Total posts: 365💌
Total words: 174500 (698 pages 📄)

When removing code duplication can go too far

I had a similar experience during a job interview.

As a part of a large test suite, I was implementing I had a test case to check if a string was an email address or not. The method that was being tested simply returned a boolean, true if the string in exam was a valid email address, false otherwise. The test consisted of checking 3 different candidate email addresses and checked if the return value from the function 'isValidEmail' was consistent.

The method was something like this (simplified for ease of reading):

NSAssertEqual("[email protected]".isValidEmail(), true)

NSAssertEqual("[email protected],commmm".isValidEmail(), false)

The interviewee complained that it could have been done with just one case using a regular expression instead of in 3 lines. While I agreed that on just a programming level it would have been better, ease of reading was highly compromised though. This was in a test so readability of the test was paramount for the future. Someone new to the project could just come in an in a few seconds realize what the test was doing.

If you just skim the file you immediately understand what's going on. They did not agree and thought that the regex was to be used.

Originally published at www.valentinourbano.com

contact: email - twitter / Terms / Privacy