These are all the most common TDD problems recruits tend to come across, read and make sure you didn’t make any of these or any mistakes at all.
Javascript:
expect(...).toThrow()
Python:
with pytest.raises([ErrorType])...
def test_something():
def totally_valid_test():
assert add(1,20,5) == 26
def another_test():
assert add(1,23,5) == 29
Note:
Nice thing about the pytest
module is that it removes the need for boilerplate code. so the following code would work:
from module import add
# tests start here.
def test_add_two_values():
assert add(1,2) == 3
def test_add_multiple_values():
assert add(1,2,3,4) == 10
Python example:
Do not do this:
def test(self):
assert add(1,2,3,4) == 10
assert multiply(1,2,3) == 6
Do this instead:
# test add function.
def test_add_two_values(self):
assert add(1,2) == 3
def test_add_multiple_values(self):
assert add(1,2,3,4,5,6) == 21
assert add(2,3,4,5) == 14
def test_multiply_two_values(self):
assert multiply(1,2) == 2
def test_multiply_multiple_values(self):
assert multiply(1,2,4) == 8
Javascript example:
Don’t do this:
describe("Test one", () => {
it("Should add", () => {
let sum = add(1, 23, 5);
expect(sum).toBe(29);
});
it("Should multiply", () => {
let prod = multiply(1, 23);
expect(prod).toBe(23);
});
});
Each Function should have it’s own describe object_
Do this:
describe("add()", () => {
it("Should add [what exactly?]", () => {
let sum = add(1, 23, 5);
expect(sum).toBe(29);
});
});
describe("multiply()", () => {
it("Should multiply [what exactly?]", () => {
let prod = multiply(1, 23);
expect(prod).toBe(23);
});
});
Javascript: Test strings are there to be descriptive:
Do: it("can/should multiply [what exactly?]")
Don’t: it("multiply")
Incomplete projects:
TDD tests fail:
Documentation on how to setup and test the code:
Clean code base. Need I say more?
Javascript:
// Testing if the Error Checks throw
//var sixSided = new Dice(6,[1,"gdfdf"]);
var sixSided = new Dice(6, [1, 6, 5, -16]);
//console.log(sixSided.rollDice());
Python:
# I was thinnking about This
# Then I did this but it didn't work. so I left
# And then, it came to me, eureka!
die6 = Dice(6)
# Since it works, there's no need to clean it.
Javascript:
var sixSided = new Dice(6, [1, 6, 5, -16]);
Python:
die6 = Dice(6)
Remove useless stuff, it serves no purpose. so get rid of it.