Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TDD FIRST principle

Tags:

tdd

I am not understanding how the TDD FIRST principle isn't being adhered to in the following code.

These are my notes about the FIRST principle:

  • Fast: run (subset of) tests quickly (since you'll be running them all the time)
  • Independent: no tests depend on others, so can run any subset in any order
  • Repeatable: run N times, get same result (to help isolate bugs and enable automation)
  • Self-checking: test can automatically detect if passed (no human checking of output)
  • Timely: written about the same time as code under test (with TDD, written first!)

The quiz question:

Sally wants her website to have a special layout on the first Tuesday of every month. She has the following controller and test code:

# HomeController  def index    if Time.now.tuesday?      render 'special_index'    else      render 'index'    end  end   # HomeControllerSpec  it "should render special template on Tuesdays" do    get 'index'    if Time.now.tuesday?      response.should render_template('special_index')    else      response.should render_template('index')    end  end 

What FIRST principle is not being followed?

  1. Fast
  2. Independent
  3. Repeatable
  4. Self-checking
  5. Timely

I'm not sure which FIRST principle is not being adhered to:

  • Fast: The code seems to be fast because there is nothing complex about its tests.
  • Independent: The test doesn't depend on other tests.
  • Repeatable: The test will get the same result every time. 'special_index' if it's Tuesday and 'index' if it's not Tuesday.
  • Self-checking: The test can automatically detect if it's passed.
  • Timely: Both the code and the test code are presented here at the same time.

I chose Timely on the quiz because the test code was presented after the controller code. But I got the question wrong, and in retrospect, this wasn't a good choice. I'm not sure which FIRST principle isn't being followed here.

like image 545
user86408 Avatar asked Aug 02 '13 19:08

user86408


People also ask

What is the first step in TDD?

In TDD, you write your unit test first, watch it fail, and then implement code changes until the test passes.

What are the principles of TDD?

Test-driven development is the practice of developing software by first writing tests and then producing the minimum amount of code required to pass those tests. That statement pretty much captures it: you write clear requirements then you write enough code to meet them, and nothing more.

What is first principle in testing?

Each test should set up its own data and should not depend on any external factors to run its test.


1 Answers

It's not Repeatable as not everyday is Tuesday :) If you run this test on Monday you will get one result, if you run it on Tuesday, a different one.

like image 62
Claudio Redi Avatar answered Sep 23 '22 20:09

Claudio Redi