Share
Embed
TDD for WS Engineering Delivery
Another "introduction to TDD" talk for work
Kerry Buckley
July 15, 2013
Transcript
70 slides
-
Test-driven development From Wikipedia, the free encyclopedia Test-driven development (TDD)
is a software development process that relies on the repetition of a very short development cycle: first the developer writes a failing automated test case that defines a desired improvement or new function, then produces code to pass that test and finally refactors the new code to acceptable standards. -
Test-driven development From Wikipedia, the free encyclopedia Test-driven development (TDD)
is a software development process that relies on the repetition of a very short development cycle: first the developer writes a failing automated test case that defines a desired improvement or new function, then produces code to pass that test and finally refactors the new code to acceptable standards. -
Test-driven development From Wikipedia, the free encyclopedia Test-driven development (TDD)
is a software development process that relies on the repetition of a very short development cycle: first the developer writes a failing automated test case that defines a desired improvement or new function, then produces code to pass that test and finally refactors the new code to acceptable standards. -
Test-driven development From Wikipedia, the free encyclopedia Test-driven development (TDD)
is a software development process that relies on the repetition of a very short development cycle: first the developer writes a failing automated test case that defines a desired improvement or new function, then produces code to pass that test and finally refactors the new code to acceptable standards. -
Test-driven development From Wikipedia, the free encyclopedia Test-driven development (TDD)
is a software development process that relies on the repetition of a very short development cycle: first the developer writes a failing automated test case that defines a desired improvement or new function, then produces code to pass that test and finally refactors the new code to acceptable standards. -
Test-driven development From Wikipedia, the free encyclopedia Test-driven development (TDD)
is a software development process that relies on the repetition of a very short development cycle: first the developer writes a failing automated test case that defines a desired improvement or new function, then produces code to pass that test and finally refactors the new code to acceptable standards. -
Anatomy of a test # Given some test accounts account_1
= Account.new(100) account_2 = Account.new(50) # When I transfer money transfer(20, from: account_1, to: account_2) # Then the balances should be updated account_1.balance.should eq(80) account_2.balance.should eq(70) ➊ Arrange ➋ Act ➌ Assert -
Acceptance tests Feature: Request the running of a night audit
report Scenario: Requesting a Night Audit Report Given I am logged in And CRM is ready to run night audit When I request a night audit report Then the night audit report should be run Scenario: Requesting another report while one is queued Given I am logged in And I have already pressed the night audit button When I request a night audit report Then it should tell me that the report is already queued Scenario: [etc...] -
Acceptance tests Feature: Request the running of a night audit
report Scenario: Requesting a Night Audit Report Given I am logged in And CRM is ready to run night audit When I request a night audit report Then the night audit report should be run Scenario: Requesting another report while one is queued Given I am logged in And I have already pressed the night audit button When I request a night audit report Then it should tell me that the report is already queued Scenario: [etc...] -
Acceptance tests When "I request a night audit report" do
click_link "Night Audit Reports" click_button "run report" end -
Unit tests describe VlanGroup, :database do describe ".with_name" do context
"when there is no group with the same name" do it "returns a new record" do expect(VlanGroup.with_name "foo").to eq VlanGroup.new(name: "foo") end end context "when there is a group with the same name" do let!(:vlan_group) { VlanGroup.create name: "foo", device_count: 1 } it "returns it" do expect(VlanGroup.with_name "foo").to eq vlan_group end end end end -
Unit tests $ rspec -f s spec/unit/models/vlan_group_spec.rb VlanGroup .with_name when
there is no group with the same name returns a new record when there is a group with the same name returns it Finished in 0.17416 seconds 2 examples, 0 failures -
Unit tests $ rspec -f s spec/unit/models/vlan_group_spec.rb VlanGroup .with_name when
there is no group with the same name returns a new record (FAILED - 1) when there is a group with the same name returns it Failures: 1) VlanGroup.with_name when there is no group with the same name returns a new record Failure/Error: expect(VlanGroup.with_name "foo").to eq VlanGroup.new(name: "foo") expected: #<VlanGroup @name="foo" @device_count=nil @url=nil> got: nil (compared using ==) # ./spec/unit/models/vlan_group_spec.rb:8:in `block (4 levels) in <top (required)>' # ./spec/support/database_hooks.rb:10:in `block (2 levels) in <top (required)>' Finished in 0.17733 seconds 2 examples, 1 failure -
Design Towards TDD 3. test-driven development Write test Write code
Release Run test Run tests Refactor -
Three laws of TDD 1. Do not write any production
code unless it is to make a failing unit test pass -
Three laws of TDD 1. Do not write any production
code unless it is to make a failing unit test pass 2. Do not write any more of a unit test than is sufficient to fail (compilation errors count!) -
Three laws of TDD 1. Do not write any production
code unless it is to make a failing unit test pass 2. Do not write any more of a unit test than is sufficient to fail (compilation errors count!) 3. Do not write any more production code than is sufficient to pass the one failing unit test -
Beyond TDD? 4. behaviour-driven development Red Refactor Green Executable examples
Release TDD Customer -
A good test… • Expresses the programmer’s intent • Gives
confidence that the code works -
A good test… • Expresses the programmer’s intent • Gives
confidence that the code works • Only tests one thing -
A good test… • Expresses the programmer’s intent • Gives
confidence that the code works • Only tests one thing • Gives clear failure messages -
A good test… • Expresses the programmer’s intent • Gives
confidence that the code works • Only tests one thing • Gives clear failure messages • Is independent of other tests -
A good test suite… • Gives confidence that the system
works • Runs quickly -
A good test suite… • Gives confidence that the system
works • Runs quickly • Is well-maintained -
A good test suite… • Gives confidence that the system
works • Runs quickly • Is well-maintained • Isolates each area under test -
Benefits for developers • Less manual testing required • Faster
feedback • ‘Safety net’ when making changes -
Benefits for developers • Less manual testing required • Faster
feedback • ‘Safety net’ when making changes • Improved design -
Benefits for developers • Less manual testing required • Faster
feedback • ‘Safety net’ when making changes • Improved design • Less code -
Benefits for developers • Less manual testing required • Faster
feedback • ‘Safety net’ when making changes • Improved design • Less code • Executable documentation -
Benefits for business • Acceptance tests clarify understanding • Frees
QA teams for exploratory testing -
Benefits for business • Acceptance tests clarify understanding • Frees
QA teams for exploratory testing • Shorter cycle time -
Benefits for business • Acceptance tests clarify understanding • Frees
QA teams for exploratory testing • Shorter cycle time • Fewer bugs -
Benefits for business • Acceptance tests clarify understanding • Frees
QA teams for exploratory testing • Shorter cycle time • Fewer bugs • Reduced rework -
How do I start? • Greenfield project? JFDI! Otherwise… •
Automate highest value tests first -
How do I start? • Greenfield project? JFDI! Otherwise… •
Automate highest value tests first • Important features -
How do I start? • Greenfield project? JFDI! Otherwise… •
Automate highest value tests first • Important features • Where the most bugs occur -
How do I start? • Greenfield project? JFDI! Otherwise… •
Automate highest value tests first • Important features • Where the most bugs occur • Use TDD for new features -
How do I start? • Greenfield project? JFDI! Otherwise… •
Automate highest value tests first • Important features • Where the most bugs occur • Use TDD for new features • Add tests for bugs when they’re found -
Warnings • TDD is hard • Don’t start learning on
production code -
Warnings • TDD is hard • Don’t start learning on
production code • Don’t forget to refactor -
Warnings • TDD is hard • Don’t start learning on
production code • Don’t forget to refactor • Look after your tests -
Warnings • TDD is hard • Don’t start learning on
production code • Don’t forget to refactor • Look after your tests • You can’t force people to do it -
Warnings • TDD is hard • Don’t start learning on
production code • Don’t forget to refactor • Look after your tests • You can’t force people to do it • Metrics lie