What's the Point
- Testing is really really really hard
- It takes valuable time
- We shouldn't spend time on hard things unless they provide value
- Always keep the value proposition in mind
- All that being said, testing is the primary tool to enable the quick delivery of high quality software.
Flavours Of Testing
- Acceptance Tests
- Unit Tests
- There are several types of testing you can do, they all accomplish different things
- Acceptance - tells you when your app is broken
- Unit - provides very quick feedback on if your class still makes sense
Properties of an Acceptance Test
- End to End
- Potentially Larger Scope
- Somewhat Fragile
- Slow
How to write an Acceptance Test
Capybara RSpec DSL
feature "Signing in" do
background do
User.make(:email => 'user@example.com', :password => 'caplin')
end
scenario "Signing in with correct credentials" do
visit '/sessions/new'
within("#session") do
fill_in 'Login', :with => 'user@example.com'
fill_in 'Password', :with => 'caplin'
end
click_link 'Sign in'
expect(page).to have_content 'Success'
end
end
When to write an acceptance test
- Usually do not test corner cases
- Not going for 100% coverage
- Cover the typical paths through the app a user would take
Unit Tests
Feedback and Code Quality
Properties of a Unit Test
- Fast
- Focused
- Clear
- Isolated
Unit tests give rapid feedback
- feedback is important
- manual checking doesnt scale
- unit tests being fast give you that feedback pretty much as you type
Unit tests expose quality issues, like tight coupling
- The act of isolating a test should be simple. When it isn't, that is a smell
- Listen to your tests, don't just man through them
How to write a Unit Test
RSpec
TBD!
How to write a Unit Test
Mocha
TBD!
Unit Testing is HARD
- Writing tests is hard, you get better by doing it
- Testing should always provide value, if it doesn't, question how to do it better
- Try not to write brittle tests, they make the value proposition worse instead of better
- Only write tests around code which could possibly break
TDD is a valuable tool
- TDD = Red => Green => Refactor
- It is a discipline that keeps you from going down bad paths
- It re-orders the way you think about problems
- At the end of the day, write tests over not writing tests
- I really like TDD, I don't use it all the time. It is a tool