Test driven development

why tests?

normal tests, written after the development, have mainly one purpose

regression

regression tests relief you code by a task: to be similar to itself

thus, they enable the code to evolve, without too many worries

but tests are terribly boring

the typical test is just "stating the obvious"

and you will never find the time for them

increase the test coverage? get more people to do that manually

i hate boring things

and we where quite stressed, in the frontend

but from May to now, i was able to keep developing and at the same time writing ~200 test specifications

and they really helped me

how to do that?

the principle

everything is based on an extremely simple principle

test

before

  • write the tests
  • test the tests: run the tests, they should fail and fail in the right way
  • develop
  • test
  • develop
  • test
  • develop
  • test...

your tests will tell you when you're done

in a very zen way, your end will meet your principle :D

also several test / dev cycles could happen, just keep in mind to revert the traditional order

every discovered bug, should be handled as a new feature

bug -> fix -- no

bug -> test -> fix -- yes

that's the only way to slowly eliminate regression problems

the wider impact

as we said, the principle is simple:

tests before

so, the first thing we get, are tests

"what do i want"? first of all

  • towards declarative
  • towards literate


describe('my component', function() {
  it('goes to detail after click on the item', function() {
    spyOn(history.newPage);
    this.component.select('$item').eq(0).click();
    expect(history.newPage)
      .toHaveBeenCalledWith({
        page: 'detail',
        item: 3243
      });
  });
});

this is the only way i know in order to think outside-in, from the very beginning

this process can lead you to always better interfaces, or not

(that means, to better testing logic instead)




expect(node.visible).toBe(true);

expect(node).toBeVisible();

final consideration, for those with an analytical mind

it's not bullet-proof

but it helps, a lot

tips from experience

for the lazy:

expect('')

let the tests write themselves

advantage? regression

grow in details when you need them

it('shows the correct value', function() {
  var $value = this.component.select('$value');
  expect(raw in this.component.data).toBe(true);
  expect(this.component.lastTemplateInput.raw)
    .toBe(0.3476);
  expect(this.component.format(0.3476))
    .toBe('34.76%');
  expect(this.component.lastTemplateInput.formatted)
    .toBe('34.76%');
  expect($value.length).toBe(1);
  expect($value.text()).toBe('34.67%');
});

yes, tests are supposed to grow, while code is supposed to shrink

at some extent

cross fire

tackle the same problem

from different angles

infrastructural reforms

from my experience, in order to switch from a normal trial and error process to a test driven one, an initial development effort is needed.


this effort is usually quite small , requiring one or two days. still you may feel quite unconfortable with your initial testing tools. convenience will come with time

questions?

if you don't have questions, why aren't you yet developing with test driven?