Testing Layers

 /andreipfeiffer
 @pfeiffer_andrei
Andrei Pfeiffer
Timisoara / RO
// code designer
// organizer, speaker
manual
________
end to end
_________________
integration
___________________________
unit
_______________________________________
few / trivial
________
-
_________________
-
___________________________
many / technical
_______________________________________
the smallest part of the SUT
method or function
unit
what to test?
API
public interface
no privates
// module pattern

const module = (() => {

    // private declarations

    return {
        foo() {};
    };
})();

...

module.foo();
// ES6 modules

// private declarations
export function foo() {};

...

import * as module from "module.js";

module.foo();
privates are "private"
// interface contract

assertEqual(getText(0), "0 products");
assertEqual(getText(1), "1 product");
assertEqual(getText(2), "2 products");
// privates are details / inditect testing

function naivePlural(value) {
    return value + 's';
}

export function getText(nr) {
    let text = 'product';
    if (nr !== 1) {
        text = naivePlural(text);
    }
    return `${nr} ${text}`;
};
// refactoring changes details

function pluralize(value) {
    return value + 's';
}

export function getText(nr) {
    let text = 'product';
    if (nr !== 1) {
        text = pluralize(text);
    }
    return `${nr} ${text}`;
};
business logic
our own code, edge cases, bugs
no 3rd party libs, framework
// N-way binding

// some viewModel
vm.list = [1, 2, 3];

...

// some view
<ul {{foreach: item as vm.list}}>
    <li>{{item}}</li>
<ul>
// N-way binding

// rendered result
<ul>
    <li>1</li>
    <li>2</li>
    <li>3</li>
<ul>
// business logic

vm.list = [1, 2, 3];

// business logic

vm.getIndex = (nr) => {
    return nr - 1;
};

white box
isolate SUT
stub / mock
how to test?
SUT
dependency
dependency
xhr / http / database / disk
SUT
dependency
dependency
xhr / http / database / disk
SUT
dependency
foo()
fn()
do()
foo()
bar()

sociable tests

dependency

Flawed Code coverage

foo()
fn()
foo()
bar()
do()
SUT

Solitary tests

SUT
dependency
fn()
do()
foo()
bar()
mock('foo');
stub('foo');
communication between
different parts of the system
integration
component testing
aka.
components communication
database
requests
disk I/O
what to test?
white box
don't isolate / use real objects
no stubs/mocks
how to test?
test database
fixtures
tests use cases, from the user's seat
end 2 end
GUI / acceptance / system
aka.
test the application
not the code
what to test?
black box
mimic real events / requests
how to test?
setup entire app
non-automated tests
manual testing
user testing
aka.
what to test?
content
semantics
correctness
style
alignment
beautifulness
user friendliness
UX
speed
slowest
________
even slower
_________________
slower
___________________________
fast
_______________________________________
Finished in 0.62 secs

SUMMARY:
52 tests completed
unit speed
35 tests passing (3s)
integration speed
6 scenarios (6 passed)
Done in 21.7 secs
e2e speed
???
________
~300x slower
_________________
~10x slower
___________________________
unit test reference
_______________________________________
unit:
integration:
end 2 end:
0.62 secs
3.00 secs
21.70 secs
unit:
integration:
end 2 end:
62 secs
5.0 mins
36.1 mins
x 100
conclusions
e2e is ideal
slow
best integration
automated
cumbersome setup
deferred run
unit testing is handy
tests only isolated code
fast
automated
simple setup
continuous run / TDD
unit
e2e
automated
integration
speed
run frequency
setup
yes
yes
high
low
low
high
low
high
complex
simple
q&a
muito obrigado

Testing part 3: Layers

By Andrei Pfeiffer

Testing part 3: Layers

Writing code without tests is like taking a sky dive without checking your parachute. But writing tests from the wrong perspective is like taking your umbrella when it’s sunny outside. This presentation aims to demystify the difference between the various layers and types of testing and why they don’t exclude each other at all.

  • 858