Lect 02
Lect 02
1 / 70
Highlights
Documenting code
APIs
unit testing
testable documentation
property-based testing
2 / 70
Highlights
3 / 70
Recap – faults, failures and errors
4 / 70
Reliability
5 / 70
What sorts of things can we test?
6 / 70
What sorts of things can we test?
We can also classify them by the purpose of the test, or when in the
software development lifecycle the testing activity occurs:
After making a change to some component (an enhancement,
or bug fix, or re-factoring): we can check whether it still passes
all relevant tests. This is called regression testing.
On delivery of a system: we can ‘test’ whether a system meets
a customer’s expectations – this is called acceptance testing
7 / 70
Testing
8 / 70
Unit testing and unit specifications
9 / 70
Documenting code
Documenting code
10 / 70
Documenting code
Documenting units
11 / 70
Documenting code
Javadoc example
That is, it takes an int and returns an int. (Why not a char? For
historical reasons we won’t get into.)
12 / 70
Documenting code
14 / 70
Documenting code
15 / 70
Documenting code
16 / 70
Documenting code
Javadoc conventions
17 / 70
Documenting code
Pydoc
In Python, the nearest equivalent method to Java’s indexOf would
be String.index, which searches for a substring within another
string.
It does not actually have Pydoc documentation, but if it did, it
would look like this:
# ...
Pydoc
Points to note:
Instead of returning -1 when the string does not occur, Python
throws an exception.
In Python, this is a typical idiom: exceptions are often thrown
to indicate the absence of something.
More on exceptions later.
19 / 70
Documenting code
Python docstrings
20 / 70
Documenting code
def myFunction(myArg):
"""
This function frobnicates the argument "myArg"
"""
# ...
21 / 70
Documenting code
# ...
22 / 70
Documenting code
APIs
1
See further “Who invented the API?”,
https://nordicapis.com/who-invented-the-api/
23 / 70
Documenting code
APIs
1
See further “Who invented the API?”,
https://nordicapis.com/who-invented-the-api/
24 / 70
Documenting code
APIs
1
See further “Who invented the API?”,
https://nordicapis.com/who-invented-the-api/
25 / 70
Documenting code
APIs as contracts
2
See further “Design by contract”,
https://en.wikipedia.org/wiki/Design_by_contract; Meyer, Bertrand. “Applying
‘design by contract’.” Computer 25.10 (1992): 40-51.
26 / 70
Documenting code
APIs, cont’d
27 / 70
Documenting code
API example
If you have used Java, you most likely at some point will have
written something like
System.out.println("some string ...")
28 / 70
Documenting code
API example
If you have used Java, you most likely at some point will have
written something like
System.out.println("some string ...")
29 / 70
Documenting code
API example
If you have used Java, you most likely at some point will have
written something like
System.out.println("some string ...")
30 / 70
Documenting code
API example
If you have used Java, you most likely at some point will have
written something like
System.out.println("some string ...")
void println(String x)
Prints a String and then terminate the line.
31 / 70
Documenting code
API example
If you have used Java, you most likely at some point will have
written something like
System.out.println("some string ...")
void println(String x)
Prints a String and then terminate the line.
32 / 70
Documenting code
The API documentation does not normally say how the function is
to be implemented – just what its return value and effects are.
This means that if the library developer decides to reimplement the
function in another way (for instance, to improve efficiency), they
can, without changing the API.
33 / 70
Documenting code
34 / 70
Documenting code
35 / 70
Documenting code
36 / 70
Documenting code
37 / 70
Documenting code
38 / 70
Documenting code
3
See also Bogosort, https://en.wikipedia.org/wiki/Bogosort
39 / 70
Documenting code
Sometimes specifications for units will describe not just what the
unit returns or does, but how it does it.
For instance, if we implement String.indexOf(ch) in the
“generate a random number” way we described, it would be
extremely slow.
40 / 70
Documenting code
41 / 70
Documenting code
42 / 70
Documenting code
APIs, summary
So:
The API describes the expected behaviour of a module (or
larger system).
The code constitutes a particular implementation of that API.
43 / 70
Documenting code
APIs, cont’d
44 / 70
Documenting code
APIs, cont’d
45 / 70
Unit tests
Unit tests
46 / 70
Unit tests
Unit tests
Unit tests should focus on one tiny bit of functionality, and attempt
to find any deviations from expected behaviour.
47 / 70
Unit tests
48 / 70
Unit tests
49 / 70
Unit tests
50 / 70
Unit tests
51 / 70
Unit tests
52 / 70
Unit tests
53 / 70
Unit tests
54 / 70
Unit tests
def fun(x):
return x + 1
class MyTest(unittest.TestCase):
def test(self):
self.assertEqual(fun(3), 4)
55 / 70
Unit tests
Unit-testing terminology
56 / 70
Unit tests
Unit-testing terminology
57 / 70
Unit tests
Unit-testing terminology
58 / 70
Unit tests
Unit-testing terminology
59 / 70
Unit tests
Test fixtures
60 / 70
Unit tests
Test fixtures
61 / 70
Unit tests
Framework features
62 / 70
Unit tests
Expected behaviour
63 / 70
Unit tests
Expected behaviour
64 / 70
Unit tests
Expected behaviour
65 / 70
Unit tests
Expected behaviour
66 / 70
Unit tests
Expected behaviour
67 / 70
Unit tests
Expected behaviour
68 / 70
Unit tests
69 / 70
Unit tests
References
70 / 70