Come to this page once your procedure runs. Running tells you it works for the inputs you happened to try. When you planned, you chose inputs on purpose and wrote down what should happen. Now you let the computer check them, every time you change anything.
Choose your test inputs
Your plan from Planning Your List and Procedure already has at least three test inputs, including a boundary case like the one in keepWords. Now make the list stronger. Copy this table onto paper, or into a file in your project, and fill it in before you run anything:
| Call (the arguments) | Why I chose it | Expected result | Actual result |
|---|---|---|---|
Aim for five to eight rows, including:
- an ordinary input, like one your clients would really give
- each branch: at least one input that makes your
iftrue and one that makes it false - the boundary: a value exactly on the edge of your condition, and one just past it
- the extremes: the smallest and largest values your app allows, and just beyond them if the app could ever pass them
- an empty list, if your procedure could ever be given one
- your acceptance criteria: look back at the acceptance criteria on your planner. Each āthenā that depends on your procedure should appear here as an expected result.
Let Xcode check them: Swift Testing
You could check each row by running the app and tapping. That works once. A test checks it every time you change anything, in a second, without tapping.
Add a test target (once per project)
If your project has no tests folder yet, do this, then commit and push:
- Choose File > New > Targetā¦.
- Choose Unit Testing Bundle, then Next.
- Make sure Testing System is Swift Testing, then Finish.
A new folder appears, ending in Tests, with one test file in it.
Write your tests
Make your own test file, named after your procedure (for example KeepDinosaursTests.swift), inside the tests folder. Turn each row of your table into a test. Here are three rows for the fictional dinosaur example from An example:
import Testing
@testable import DinosaurFinder
@MainActor
struct KeepDinosaursTests {
// MARK: - Functions
@Test func lengthZeroKeepsEveryDinosaur() {
let result: [Dinosaur] = keepDinosaurs(longerThan: 0, from: allDinosaurs)
#expect(result.count == 3)
}
@Test func exactlyNineIsNotLongerThanNine() {
let result: [Dinosaur] = keepDinosaurs(longerThan: 9, from: allDinosaurs)
#expect(result.count == 1)
#expect(result[0].name == "Spinosaurus")
}
@Test func emptyListGivesEmptyResult() {
let result: [Dinosaur] = keepDinosaurs(longerThan: 5, from: [])
#expect(result.isEmpty)
}
}@testable importis followed by your appās name as it appears in the tests file Xcode made for you.@MainActorlets your tests use your list and procedure, which live with your appās screens.- Each
@Testis one row of your table. Name it after what it checks. #expect(...)holds the expected result from your table.
Press Command-U to run every test. A green diamond beside a test means it passed. A red one means the actual result was different, and Xcode shows both values. For example:
Expectation failed: (spacesLeft(capacity: 10, booked: 12) ā -2) == 0says the call gave -2 where you expected 0.
When a test fails
Stop and decide which one is wrong: your procedure, or your expectation? Reread your plan and your acceptance criteria before you change anything. If the procedure is wrong, fix it, and run the tests again. If your expectation was wrong, fix the test and write down why in your table.
Fill in the āActual resultā column as you go. Commit your tests along with your code.
Curriculum connection
CRD-2.J
Identify inputs and corresponding expected outputs or behaviors that can be used to check the correctness of an algorithm or program.
CRD-2.J.1 In the development process, testing uses defined inputs to ensure that an algorithm or program is producing the expected outcomes. Programmers use the results from testing to revise their algorithms or programs.
CRD-2.J.2 Defined inputs used to test a program should demonstrate the different expected outcomes that are at or just beyond the extremes (minimum and maximum) of input data.
CRD-2.J.3 Program requirements are needed to identify appropriate defined inputs for testing.
Link to originalCRD-2.I
For errors in an algorithm or program: a. Identify the error. b. Correct the error.
CRD-2.I.1 A logic error is a mistake in the algorithm or program that causes it to behave incorrectly or unexpectedly.
CRD-2.I.2 A syntax error is a mistake in the program where the rules of the programming language are not followed.
CRD-2.I.3 A run-time error is a mistake in the program that occurs during the execution of a program. Programming languages define their own run-time errors.
CRD-2.I.4 An overflow error is an error that occurs when a computer attempts to handle a number that is outside of the defined range of values.
CRD-2.I.5 The following are effective ways to find and correct errors:
Link to original
- test cases
- hand tracing
- visualizations
- debuggers
- adding extra output statement(s) )
AAP-3.A
For procedure calls: a. Write statements to call procedures. b. Determine the result or effect of a procedure call.
AAP-3.A.1 A procedure is a named group of programming instructions that may have parameters and return values.
AAP-3.A.2 Procedures are referred to by different names, such as method or function, depending on the programming language.
AAP-3.A.3 Parameters are input variables of a procedure. Arguments specify the values of the parameters when a procedure is called.
AAP-3.A.4 A procedure call interrupts the sequential execution of statements, causing the program to execute the statements within the procedure before continuing. Once the last statement in the procedure (or a return statement) has executed, flow of control is returned to the point immediately following where the procedure was called.
AAP-3.A.5 The exam reference sheet provides
procName(arg1, arg2, ...)as a way to call
Text:
PROCEDURE procName(parameter1, parameter2, ...) { <block of statements> }Block:
PROCEDURE procName parameter1, parameter2,... block of statementswhich takes zero or more arguments;
arg1is assigned toparameter1,arg2is assigned toparameter2, and so on.AAP-3.A.6 The exam reference sheet provides the procedure
Text:
DISPLAY(expression)Block:
DISPLAY expressionto display the value of
expression, followed by a space.AAP-3.A.7 The exam reference sheet provides the
Text:
RETURN(expression)Block:
RETURN expressionstatement, which is used to return the flow of control to the point where the procedure was called and to return the value of
expression.AAP-3.A.8 The exam reference sheet provides
result ā procName(arg1, arg2, ...)to assign to
resultthe āvalue of the procedureā being returned by callingText:
PROCEDURE procName(parameter1, parameter2, ...) { <block of statements> RETURN(expression) }Block:
PROCEDURE procName parameter1, parameter2,... block of statements RETURN expressionAAP-3.A.9 The exam reference sheet provides procedure
Text:
INPUT()Block:
INPUTwhich accepts a value from the user and returns the input value.
Link to originalA4.2
develop and implement a formal testing plan (e.g., unit testing, integration testing, regression testing) for a software project to ensure program correctness;
Link to original