Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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"
    ..
share|improve this question

closed as not a real question by gnat, Mark Trapp, Yusubov, GlenH7, StuperUser Oct 17 '12 at 12:40

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

    
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. –  GlenH7 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. –  GlenH7 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

3 Answers 3

up vote 4 down vote accepted

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.

share|improve this answer

I really do like the AAA pattern.

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

// Act 
test

// Assert
validate test
share|improve this answer
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

share|improve this answer

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