Doing unit testing for the first time at a large scale, I find myself writing a lot of repetitive unit tests for my business logic. Sure, to create complete test suites I need to test all possibilities but readability feels compromised doing what I do - as shown in the psuedocode below.

How would a well written, readable test suit look like?

describe "UserEntity" ->
  it "valid name validates"
  it "invalid name doesnt validate"

  it "valid list of followers validate"
  • Have you tried searching through P.SE using "Unit testing" for the search terms? I don't see how your question is building upon the number of already answered questions on this subject. – user53019 Oct 16 '12 at 14:41
  • Yes, but I have not seen any good answers on how readability is maintained – Industrial Oct 16 '12 at 15:32
  • It would help your question if you linked to some of the better answers you have found and explain why they aren't answering your particular question. As it stands, there are currently 4 close votes on this question which is most likely due to the overly broad nature of the question. – user53019 Oct 16 '12 at 15:38
  • 1
    Depending on what type of code you're testing and what framework for unit tests, the answer will be different. I'm only really familiar with the XUnit tests patterns, and there is a lot of good advice for that in amazon.co.uk/Art-Unit-Testing-Examples-NET/dp/1933988274 – StuperUser Oct 17 '12 at 12:42

In general, a well written test suite inevitably contains some repetition, as you are testing the same method over and over with different parameters and results. But if you have significant chunks of duplicate test code, you can improve it by refactoring. Extract repeated code parts into separate methods which you can then reuse.

For a more detailed treatment, check out Growing Object-Oriented Software Guided by Tests. It contains lots of tips and practical examples on how to make your test code more concise, fluent and readable.


I really do like the AAA pattern.

// Arrange
arrange needed mocks, stubs, parameters, whatever

// Act 

// Assert
validate test
  • 3
    AAA is great for frameworks that encourage it (xUnit, etc). But the language in the question suggests a Context/Specification-style framework, which doesn't really fit. – pdr Oct 16 '12 at 10:08

in my eyes a good test looks like:

description of what is tested (include mentions of whether it's general case, edge case, exceptional case)

include references to any relevant bug reports
void testSomthing(){
    prepare mocked behavior (if any)

    set the input for the method

    set expected_output from the test

    gotten_output=do call to tested method

    assertEquals(gotten_output, expected_output);

repeat for each test

without the documentation you won't know which of the 2 dozen tests on a method test what

Not the answer you're looking for? Browse other questions tagged or ask your own question.