Skip to the content.

Introduction to TDD

-

What we’ll cover

-

3 laws of TDD

First Law

You may not write production code until you have a written a failing corresponding unit test.

public class cow{
	public cow(){}
	
	public String speak(){
		//BAD DOBBY
		return "Mooooow";
	}
}

-

First Law

You may not write production code until you have a written a failing corresponding unit test.

public class cow{
	public cow(){}
	
	public String speak(){
		//Good DOBBY
		return null;
	}
}

-

public class cowTest{
	
	@Test
	public void speakTest(){
		Cow cow = new Cow();
		String expected = "mooo";
		String actual = cow.speak();
		Assert.equals(expected, actual);
	}
}

-

3 laws of TDD

Second Law

You may not write more of a unit test than is needed to fail, and not compiling is failing.

@Test
public void speakTest(){
	Cow cow = new Cow();
	Cow cow2 = new Cow();
	
	String expected = "mooo";
	String actual = cow.speak();
	
	String expected2 = "mooo";
	String actual2 = cow2.speak();
	Assert.equals(expected2, actual2);
	// BAD DOBBY
	Assert.equals(actual, actual2);
}

-

3 laws of TDD

Third Law

You may not write more production code than is sufficent to pass current failing test.

public String speak(){
	String sound = "Mooooo";
	//BAD DOBBY!!!!
	String bucketsOfMilk = this.milkMe();
	return sound;
}

-

Clean Code and Clean Test

A clean test is a readable Test

@Test
public void testFunction(){
//Bad Dobby
Unicorn z = new Unicorn();
int x = 23;
int y = z.saveThePrincess();
Assert.equals(x,y);
}

-

@Test
public void testCountToTwo(){
	//Good Dobby
	NumberCounter counterObject = new NumberCounter();
	int expectedNumber = 30000000;
	int actualNumber = counterObject.countToThreeMillion();
	Assert.equals(expectedNumber, actualNumber);
}

-

One Assertion to Rule them All

A good unit test should only come to one binary conclusion, which should also be quick and easy to understand.

Remember: SINGLE RESPONSIBLITY

Single Concept per test, with the following:

-

F.I.R.S.T

-

Demo Time!