Presented at Web Unleashed on September 16-17, 2015 in Toronto, Canada
More info at www.fitc.ca/webu
The Little Shop of TDD Horrors
with Giorgio Natili
OVERVIEW
When used properly, the TDD (Test Driven Development) cycle is one of the most effective ways to improve the efficiency of a team and overall code quality. However, most of the time, misuse of this powerful technique brings unsatisfactory results. During this talk we’ll explore how to identify “testing smells” and how to prevent bad tests that negatively impact the design and architecture of a Web app.
We’ll investigate some real world mobile app examples in JavaScript, Swift, and Java for Android that threaten to eat us alive!
OBJECTIVE
Learn to write effective tests for your apps.
TARGET AUDIENCE
Software and QA Engineers.
ASSUMED AUDIENCE KNOWLEDGE
Good understanding of software development and a basic knowledge of test driven development.
FIVE THINGS AUDIENCE MEMBERS WILL LEARN
Write clean tests
Dealing with legacy code
The differences between unit tests and behaviour tests
How to debug a test
How to keep the test code base clean
7. @giorgionatili #mobiletea 7
!
Setup and Teardown
Tests clarity and readability
Mocking data
Contract tests
Behaviors driven development
End to end testing
The goals of testing
What we’ll talk about
8. “We are going to cycle in good and bad practices highlighting
potential solutions…”
How we’ll talk about this
19. @giorgionatili #mobiletea 19
Eventually decouple setups
Just in case you really need complicated setups
decouple them from tests and include them in the
setup…
26. @giorgionatili #mobiletea 26
Tests are code, treat them well
Write small, readable and decoupled tests. Tests are
part of your code base, don’t write garbage code!
35. @giorgionatili #mobiletea 35
Respect the privacy
Don’t expose methods just because you have to test
it: well-designed public methods should use them in
such a way they don’t need be directly tested
45. @giorgionatili #mobiletea 45
Poor descriptions
Tests descriptions are one of the best sources of
documentation, never ignore the importance of
providing a good description
47. @giorgionatili #mobiletea 47
Arrange, Act, Assert (AAA)
Separate what is going to be tested from the setups
and keep the verification steps clearly identified
49. @giorgionatili #mobiletea 49
Avoid conditional behaviors
If you start to add conditionals to determine if your
code is executed by the tests suites, it means that
you are in danger
55. @giorgionatili #mobiletea 55
Network Responses
Applications rely on external data sources, it’s
important to have clear expectations about external
APIs responses
59. @giorgionatili #mobiletea 59
'use strict';
var supertest = require('supertest-as-promised');
var request = supertest('https://ngcourse.herokuapp.com');
var assert = require('chai').assert;
describe('Backend API', function() {
it ('should return the list of users', function() {
return request.get('/api/v1/users')
.expect(200);
});
});
Contract Tests in Practice
64. @giorgionatili #mobiletea 64
Divide tests logically
Never violate the Single Responsibility Principle
neither when writing tests, keep the ordered and in a
good shape is the key to get the more value from
them…
65. @giorgionatili #mobiletea 65
Measure complexity
If you are not able to organize your tests in such a
way it means that you are dealing with confusing
(and poor!) requirements
66. @giorgionatili #mobiletea 66
Scenarios -> Use Cases -> Behaviors
This connection is the key to defining tests that
clearly describe a feature and its acceptance criteria
68. @giorgionatili #mobiletea 68
Test for pixels
Don’t use e2e tests for testing layout
implementations, automate (eventually) screenshots
and then examine them
69. @giorgionatili #mobiletea 69
Counting elements
Don’t use e2e testing to count UI elements, use
them to describe how the UI *should change* with a
specific behavior
70. @giorgionatili #mobiletea 70
Writing E2E tests
Never wait too much, start to write end user tests as
soon as the requirements are ready, the lack of
automation can bring to not reliable outputs
75. @giorgionatili #mobiletea 75
Common understanding
Don’t separate developers and testers! Keeping
them together allows you to have testers that
understand the app internals