Lect03 Autom
Lect03 Autom
1 / 111
Test automation Test cases
Re-cap
2 / 111
Test automation Test cases
Questions
3 / 111
Test automation Test cases
Questions
4 / 111
Test automation Test cases
Test structure
5 / 111
Test automation Test cases
6 / 111
Test automation Test cases
7 / 111
Test automation Test cases
9 / 111
Test automation Test cases
10 / 111
Test automation Test cases
11 / 111
Test automation Test cases
12 / 111
Test automation Test cases
13 / 111
Test automation Test cases
Questions
requirements system/subsystem-test
14 / 111
Test automation Test cases
Unit tests are at the bottom of the hierarchy, and directly test
small parts of the system created during system
implementation.
They should have the properties we said all good unit tests
should have (independent, quick to run), and should be run
frequently as a project progresses. (e.g. for every change we
make to a class)
15 / 111
Test automation Test cases
16 / 111
Test automation Test cases
Questions
Both of these are covered in the next few lectures. We look at ways
of grouping together different sorts of input so that we don’t need
to test exhaustively, and at ways of working out how much of the
system we have tested (and ought to test).
17 / 111
Test automation Test cases
18 / 111
Test automation Test cases
19 / 111
Test automation Test cases
21 / 111
Test automation Test cases
Questions
22 / 111
Test automation Test cases
Questions
23 / 111
Test automation Test cases
Questions
24 / 111
Test automation Test cases
Coming up
25 / 111
Test automation Test cases
Coming up
26 / 111
Test automation Test cases
Test automation
27 / 111
Test automation Test cases
Testing frameworks
28 / 111
Test automation Test cases
Testing frameworks
29 / 111
Test automation Test cases
Testing frameworks
30 / 111
Test automation Test cases
Testing frameworks
Besides the fact that we can use them for unit tests, JUnit and
the other xUnit frameworks are good examples of testing
frameworks, or test automation frameworks.
Amman and Offut define test automation as:
Testing frameworks
32 / 111
Test automation Test cases
Testing frameworks
33 / 111
Test automation Test cases
Testing frameworks
34 / 111
Test automation Test cases
Testing frameworks
35 / 111
Test automation Test cases
Testing frameworks
36 / 111
Test automation Test cases
Testing frameworks
37 / 111
Test automation Test cases
Testing frameworks
(. . . continued):
38 / 111
Test automation Test cases
Testing frameworks
39 / 111
Test automation Test cases
Testing frameworks
40 / 111
Test automation Test cases
Testing frameworks
41 / 111
Test automation Test cases
Testing frameworks
42 / 111
Test automation Test cases
Test cases
43 / 111
Test automation Test cases
44 / 111
Test automation Test cases
45 / 111
Test automation Test cases
46 / 111
Test automation Test cases
Definitions:
Test values: input values necessary to complete some execution
of the software.
Expected values: result to be produced iff program satisfies
intended behaviour on a test case.
47 / 111
Test automation Test cases
48 / 111
Test automation Test cases
Prefix values
49 / 111
Test automation Test cases
50 / 111
Test automation Test cases
Verification values
51 / 111
Test automation Test cases
Exit commands
If our software made use of, say, a database – then ‘exit commands’
might include commands needed to restore the database to a
known, stable state.
52 / 111
Test automation Test cases
53 / 111
Test automation Test cases
Test fixtures
54 / 111
Test automation Test cases
55 / 111
Test automation Test cases
Some systems are very easy to observe and control, others less
so.
56 / 111
Test automation Test cases
57 / 111
Test automation Test cases
58 / 111
Test automation Test cases
59 / 111
Test automation Test cases
60 / 111
Test automation Test cases
61 / 111
Test automation Test cases
62 / 111
Test automation Test cases
63 / 111
Test automation Test cases
64 / 111
Test automation Test cases
unittest
class KnownInput(unittest.TestCase):
knownValues = (('lego', False),('radar', True))
def testKnownValues(self):
for word, palin in self.knownValues:
result = is_palindrome(word)
self.assertEqual(result, palin)
65 / 111
Test automation Test cases
Test fixtures
Recall that test fixtures are things we need in order to get the
system into a known state, ready for a test
Often, multiple tests will share some requirements for what
environment needs to be set up
A typical approach in object-oriented languages is to group
tests with shared fixture requirements into the same class
And then to specify “setup” and “tear-down” methods for the
class, which will be run before and after each test, respectively.
Shared objects will be declared as instance variables
66 / 111
Test automation Test cases
Test fixtures
class TestArithmeticOperations {
Calculator myCalculator;
@Test
void test1() {
// ...
67 / 111
Test automation Test cases
Fixtures in Python
import unittest
class FixturesTest(unittest.TestCase):
def setUp(self):
print('In setUp()')
self.fixture = range(1, 10)
def tearDown(self):
print('In tearDown()')
del self.fixture
def test(self):
print('in test()')
self.assertEqual(self.fixture, range(1, 10))
if __name__ == '__main__':
unittest.main()
68 / 111
Test automation Test cases
Common assertions
assertTrue(x, msg=None)
assertFalse(x, msg=None)
assertIsNone(x, msg=None)
assertIsNotNone(x, msg=None)
assertEqual(a, b, msg=None)
assertNotEqual(a, b, msg=None)
assertIs(a, b, msg=None)
assertIsNot(a, b, msg=None)
assertIn(a, b, msg=None)
assertNotIn(a, b, msg=None)
assertIsInstance(a, b, msg=None)
assertNotIsInstance(a, b, msg=None)
69 / 111
Test automation Test cases
Other assertions
assertAlmostEqual(a, b, places=7, msg=None, delta=None)
assertNotAlmostEqual(a, b, places=7, msg=None, delta=None)
assertGreater(a, b, msg=None)
assertGreaterEqual(a, b, msg=None)
assertLess(a, b, msg=None)
assertLessEqual(a, b, msg=None)
assertRegex(text, regexp, msg=None)
assertNotRegex(text, regexp, msg=None)
assertCountEqual(a, b, msg=None)
assertMultiLineEqual(a, b, msg=None)
assertSequenceEqual(a, b, msg=None)
assertListEqual(a, b, msg=None)
assertTupleEqual(a, b, msg=None)
assertDictEqual(a, b, msg=None)
70 / 111
Test automation Test cases
Running Tests
class SimplisticTest(unittest.TestCase):
def test(self):
self.assertTrue(True)
if __name__ == '__main__':
unittest.main()
71 / 111
Test automation Test cases
Running Tests
.
---------------------------------------------------------
Ran 1 test in 0.000s
OK
72 / 111
Test automation Test cases
# ...
74 / 111
Test automation Test cases
75 / 111
Test automation Test cases
Doubles
76 / 111
Test automation Test cases
77 / 111
Test automation Test cases
Dependencies
78 / 111
Test automation Test cases
Often, we’ll use objects or function that mimic other ones for
testing purposes. There does not seem to be any universally
accepted term for these, but one author [Gerard Meszaros] uses
the generic term “Test Double”.
Specific sorts of Test Double -
Dummy objects
Fake objects
Stubs
Spies
Mocks
79 / 111
Test automation Test cases
Dummy objects
These are objects that are passed around but not used – for
instance, they may be used to fill parameter lists (in statically
typed languages).
In languages with a null, Nil or undefined value, we might
be able to use that value
(which also serves to document the fact that we don’t care
what it is)
80 / 111
Test automation Test cases
Fake objects
81 / 111
Test automation Test cases
Stubs
82 / 111
Test automation Test cases
Spies
83 / 111
Test automation Test cases
Spies – example
84 / 111
Test automation Test cases
Mocks
85 / 111
Test automation Test cases
Mocks
86 / 111
Test automation Test cases
Mocks
87 / 111
Test automation Test cases
Mocks
88 / 111
Test automation Test cases
Mocks
89 / 111
Test automation Test cases
90 / 111
Test automation Test cases
Mocks in Python
91 / 111
Test automation Test cases
Once we have called our mock() object, the fact that it has
been called is recorded.
We then (before the test ends) assert what we expect to have
happened
(e.g. that the method was called)
If not, then an exception will be raised.
Much more complex behaviour can be created – check the API
for details.
92 / 111
Test automation Test cases
Testable documentation
93 / 111
Test automation Test cases
94 / 111
Test automation Test cases
Doctest example
def square(x):
"""Return the square of x.
>>> square(2)
4
>>> square(-2)
4
"""
return x * x
if __name__ == '__main__':
import doctest
doctest.testmod()
95 / 111
Test automation Test cases
96 / 111
Test automation Test cases
97 / 111
Test automation Test cases
98 / 111
Test automation Test cases
99 / 111
Test automation Test cases
Property-based testing
100 / 111
Test automation Test cases
Property-based testing
101 / 111
Test automation Test cases
Simple example
102 / 111
Test automation Test cases
Simple example
103 / 111
Test automation Test cases
104 / 111
Test automation Test cases
105 / 111
Test automation Test cases
106 / 111
Test automation Test cases
107 / 111
Test automation Test cases
108 / 111
Test automation Test cases
109 / 111
Test automation Test cases
Double of
dependency
component
110 / 111
Test automation Test cases
Next
111 / 111