Treating test code like production code

Treating test code like production code#

Yes, as I already mentioned I love tests. Tests should be treated like the production code they test, right? So They should also be refactored the same way. There should also be time for, and the business side should IMHO understand the importance of it. Sure the business value is not measurable at the moment, but it will be later. Especially when the test code has become a lot of copy-pasting around and engineers don’t feel like touching the test code anymore, because it has become kind of complex. We all know it, and we should do it, but sometimes we are all lazy. This post is a friendly reminder to me to be more strict with this.

Treat and refactor your test code the same way you do it with the production code

Tests should be easy to read and follow the 3 As from

  1. arrange: Set up the object to be tested. Set up collaborators who might be test objects (mocks, etc.).
  2. act: on the object under test.
  3. assert: that the expected results have occurred.

Moreover, test methods should never depend on each other, or the order, or on a global state. Keep them small and if you have to start scrolling then probably the test has become to complex or tests to many things or you need a larger screen :P


2 simple steps helped me clean up tests: Categorization and Factory Methods

describe('External adapter requests', () => {
    describe('AB adapter', () => { // => easier to read 
        const userId = uuid.v4()
        const cardId = uuid.v4()

        it('should create card in database', async () => {
          await setupEnrichmentMock(userId, cardId)
          // => abstracted into a factory method
          await sendRequestToAdapter(userId, cardId) 

          const cards = await
            .where({ card_id: cardId })
          const card = cards[0]  


Although it might seem to be easier to copy past code around simply, it will pull you back. Not after some hours but at least after a couple of days when you need to touch this code again. Not only the other engineers will love you for this, but also your future you will do it.


I stumbled over this repo git-flight-rules this week and TBH, this document is so helpful. Finally, a document which condenses a lot of StackOverflow post and large parts of the git documentation down into a single document.

© 2021