1
- :orphan:
2
1
3
- .. @raise litre.TestsAreMissing
4
-
5
- =============
6
- Testing Swift
7
- =============
2
+ # Testing Swift
8
3
9
4
This document describes how we test the Swift compiler, the Swift runtime, and
10
5
the Swift standard library.
11
6
12
- Testing approaches
13
- ==================
7
+ ## Testing approaches
14
8
15
9
We use multiple approaches to test the Swift toolchain.
16
10
17
11
* LLVM lit-based testsuites for the compiler, runtime and the standard library.
18
-
19
12
* Unit tests for sub-tools.
20
-
21
13
* A selection of open source projects written in Swift.
22
14
23
- The LLVM lit-based testsuite
24
- ============================
15
+ ## The LLVM lit-based testsuite
25
16
26
17
** Purpose** : primary testsuites for the Swift toolchain.
27
18
@@ -32,27 +23,21 @@ The LLVM lit-based testsuite
32
23
* Engineers and contributors are expected to run tests from these testsuites
33
24
locally before committing. (Usually on a single platform, and not necessarily
34
25
all tests.)
35
-
36
26
* Buildbots run all tests, on all supported platforms.
37
27
38
- Testsuite subsets
39
- -----------------
28
+ ### Testsuite subsets
40
29
41
30
The testsuite is split into four subsets:
42
31
43
32
* Primary testsuite, located under `` swift/test `` .
44
-
45
33
* Validation testsuite, located under `` swift/validation-test `` .
46
-
47
34
* Unit tests, located under `` swift/unittests `` .
48
-
49
35
* Long tests, which are marked with `` REQUIRES: long_test `` .
50
36
51
- Unlike other tests, every long test should also include either
52
- ``REQUIRES: nonexecutable_test `` or ``REQUIRES: executable_test ``.
37
+ Unlike other tests, every long test should also include either
38
+ `` REQUIRES: nonexecutable_test `` or `` REQUIRES: executable_test `` .
53
39
54
- Running the LLVM lit-based testsuite
55
- ------------------------------------
40
+ ### Running the LLVM lit-based testsuite
56
41
57
42
It is recommended that you run the Swift test suites via `` utils/build-script `` .
58
43
For day-to-day work on the Swift compiler, using `` utils/build-script --test ``
@@ -68,66 +53,45 @@ technically possible to execute the tests directly via CMake. For example, if yo
68
53
built Swift products at the directory `` build/Ninja-ReleaseAssert/swift-macosx-x86_64 `` ,
69
54
you may run the entire test suite directly using the following command:
70
55
71
- .. code-block :: bash
72
-
56
+ ```
73
57
cmake --build build/Ninja-ReleaseAssert/swift-macosx-x86_64 -- check-swift-macosx-x86_64
58
+ ```
74
59
75
60
Note that `` check-swift `` is suffixed with a target operating system and architecture.
76
61
Besides `` check-swift `` , other targets are also available. Here's the full list:
77
62
78
- * ``check-swift ``
79
-
80
- Runs tests from the ``${SWIFT_SOURCE_ROOT}/test `` directory.
81
-
82
- * ``check-swift-only_validation ``
83
-
84
- Runs tests from the ``${SWIFT_SOURCE_ROOT}/validation-test `` directory.
85
-
86
- * ``check-swift-validation ``
87
-
88
- Runs the primary and validation tests, without the long tests.
89
-
90
- * ``check-swift-only_long ``
91
-
92
- Runs long tests only.
93
-
94
- * ``check-swift-all ``
95
-
96
- Runs all tests (primary, validation, and long).
97
-
98
- * ``SwiftUnitTests ``
99
-
100
- Builds all unit tests. Executables are located under
63
+ * `` check-swift `` : Runs tests from the `` ${SWIFT_SOURCE_ROOT}/test `` directory.
64
+ * `` check-swift-only_validation `` : Runs tests from the `` ${SWIFT_SOURCE_ROOT}/validation-test `` directory.
65
+ * `` check-swift-validation `` : Runs the primary and validation tests, without the long tests.
66
+ * `` check-swift-only_long `` : Runs long tests only.
67
+ * `` check-swift-all `` : Runs all tests (primary, validation, and long).
68
+ * `` SwiftUnitTests `` : Builds all unit tests. Executables are located under
101
69
`` ${SWIFT_BUILD_ROOT}/unittests `` and must be run individually.
102
70
103
71
For every target above, there are variants for different optimizations:
104
72
105
73
* the target itself (e.g., `` check-swift `` ) -- runs all tests from the primary
106
74
testsuite. The execution tests are run in `` -Onone `` mode.
107
-
108
75
* the target with `` -optimize `` suffix (e.g., `` check-swift-optimize `` ) -- runs
109
76
execution tests in `` -O `` mode. This target will only run tests marked as
110
77
`` executable_test `` .
111
-
112
78
* the target with `` -optimize-unchecked `` suffix (e.g.,
113
79
`` check-swift-optimize-unchecked `` ) -- runs execution tests in
114
80
`` -Ounchecked `` mode. This target will only run tests marked as
115
81
`` executable_test `` .
116
-
117
82
* the target with `` -executable `` suffix (e.g.,
118
83
`` check-swift-executable-iphoneos-arm64 `` ) -- runs tests marked with
119
84
`` executable_test `` in `` -Onone `` mode.
120
-
121
85
* the target with `` -non-executable `` suffix (e.g.,
122
86
`` check-swift-non-executable-iphoneos-arm64 `` ) -- runs tests not marked with
123
87
`` executable_test `` in `` -Onone `` mode.
124
88
125
89
If more control is required (e.g. to manually run certain tests), you can invoke
126
90
LLVM's lit.py script directly. For example:
127
91
128
- .. code-block :: bash
129
-
92
+ ```
130
93
% ${LLVM_SOURCE_ROOT}/utils/lit/lit.py -sv ${SWIFT_BUILD_ROOT}/test-iphonesimulator-i386/Parse/
94
+ ```
131
95
132
96
This runs the tests in the test/Parse/ directory targeting the 32-bit iOS
133
97
Simulator. The `` -sv `` options give you a nice progress bar and only show you
@@ -141,60 +105,45 @@ source 'test/' directory.) There is a more verbose form that specifies the
141
105
testing configuration explicitly, which then allows you to test files
142
106
regardless of location.
143
107
144
- .. code-block :: bash
145
-
108
+ ```
146
109
% ${LLVM_SOURCE_ROOT}/utils/lit/lit.py -sv --param swift_site_config=${SWIFT_BUILD_ROOT}/test-iphonesimulator-i386/lit.site.cfg ${SWIFT_SOURCE_ROOT}/test/Parse/
110
+ ```
147
111
148
112
For more complicated configuration, copy the invocation from one of the build
149
113
targets mentioned above and modify it as necessary. lit.py also has several
150
114
useful features, like timing tests and providing a timeout. Check these features
151
115
out with `` lit.py -h `` . We document some of the more useful ones below:
152
116
153
- Extra lit.py invocation options
154
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
117
+ #### Extra lit.py invocation options
155
118
156
119
* `` -s `` reduces the amount of output that lit shows.
157
-
158
120
* `` -v `` causes a test's commandline and output to be printed if the test fails.
159
-
160
121
* `` -a `` causes a test's commandline and output to always be printed.
161
-
162
122
* `` --filter=<pattern> `` causes only tests with paths matching the given regular
163
123
expression to be run.
164
-
165
124
* `` -i `` causes tests that have a newer modification date and failing tests to
166
125
be run first. This is implemented by updating the mtimes of the tests.
167
-
168
126
* `` --no-execute `` causes a dry run to be performed. * NOTE* This means that all
169
127
tests are assumed to PASS.
170
-
171
128
* `` --time-tests `` will cause elapsed wall time to be tracked for each test.
172
-
173
129
* `` --timeout=<MAXINDIVIDUALTESTTIME> `` sets a maximum time that can be spent
174
130
running a single test (in seconds). 0 (the default means no time limit.
175
-
176
131
* `` --max-failures=<MAXFAILURES> `` stops execution after `` MAXFAILURES `` number
177
132
of failures.
178
-
179
133
* `` --param gmalloc `` will run all tests under Guard Malloc (macOS only). See
180
134
`` man libgmalloc `` for more information.
181
-
182
135
* `` --param swift-version=<MAJOR> `` overrides the default Swift language
183
136
version used by swift/swiftc and swift-ide-test.
184
-
185
137
* `` --param interpret `` is an experimental option for running execution tests
186
138
using Swift's interpreter rather than compiling them first. Note that this
187
139
does not affect all substitutions.
188
-
189
140
* `` --param swift_test_mode=<MODE> `` drives the various suffix variations
190
141
mentioned above. Again, it's best to get the invocation from the existing
191
142
build system targets and modify it rather than constructing it yourself.
192
143
193
- Writing tests
194
- -------------
144
+ ### Writing tests
195
145
196
- General guidelines
197
- ^^^^^^^^^^^^^^^^^^
146
+ #### General guidelines
198
147
199
148
When adding a new testcase, try to find an existing test file focused on the
200
149
same topic rather than starting a new test file. There is a fixed runtime cost
@@ -225,7 +174,6 @@ details of the standard library (unless doing so is point of the test).
225
174
Platform-dependent details include:
226
175
227
176
* `` Int `` (use integer types with explicit types instead).
228
-
229
177
* Layout of `` String `` , `` Array `` , `` Dictionary `` , `` Set `` . These differ
230
178
between platforms that have Objective-C interop and those that don't.
231
179
@@ -239,8 +187,7 @@ standard library that only has a very basic set of APIs.
239
187
If you write an executable test please add `` REQUIRES: executable_test `` to the
240
188
test.
241
189
242
- Substitutions in lit tests
243
- ^^^^^^^^^^^^^^^^^^^^^^^^^^
190
+ #### Substitutions in lit tests
244
191
245
192
Substitutions that start with `` %target `` configure the compiler for building
246
193
code for the target that is not the build machine:
@@ -427,6 +374,7 @@ Other substitutions:
427
374
When writing a test where output (or IR, SIL) depends on the bitness of the
428
375
target CPU, use this pattern::
429
376
377
+ ```
430
378
// RUN: %target-swift-frontend ... | %FileCheck --check-prefix=CHECK --check-prefix=CHECK-%target-ptrsize %s
431
379
432
380
// CHECK: common line
@@ -439,10 +387,12 @@ target CPU, use this pattern::
439
387
// CHECK: define @foo() {
440
388
// CHECK-32: integer_literal $Builtin.Int32, 0
441
389
// CHECK-64: integer_literal $Builtin.Int64, 0
390
+ ```
442
391
443
392
When writing a test where output (or IR, SIL) depends on the target CPU itself,
444
393
use this pattern::
445
394
395
+ ```
446
396
// RUN: %target-swift-frontend ... | %FileCheck --check-prefix=CHECK --check-prefix=CHECK-%target-cpu %s
447
397
448
398
// CHECK: common line
@@ -452,9 +402,9 @@ use this pattern::
452
402
// CHECK-arm64: only for arm64
453
403
// CHECK-powerpc64: only for powerpc64
454
404
// CHECK-powerpc64le: only for powerpc64le
405
+ ```
455
406
456
- Features for ``REQUIRES `` and ``XFAIL ``
457
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
407
+ #### Features for `` REQUIRES `` and `` XFAIL ``
458
408
459
409
FIXME: full list.
460
410
@@ -476,25 +426,23 @@ FIXME: full list.
476
426
* `` XFAIL: linux `` : tests that need to be adapted for Linux, for example parts
477
427
that depend on Objective-C interop need to be split out.
478
428
479
- Feature ``REQUIRES: executable_test ``
480
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
429
+ #### Feature `` REQUIRES: executable_test ``
481
430
482
431
This feature marks an executable test. The test harness makes this feature
483
432
generally available. It can be used to restrict the set of tests to run.
484
433
485
- StdlibUnittest
486
- ^^^^^^^^^^^^^^
434
+ #### StdlibUnittest
487
435
488
436
Tests accept command line parameters, run StdlibUnittest-based test binary
489
437
with `` --help `` for more information.
490
438
491
- Testing memory management in execution tests
492
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
439
+ #### Testing memory management in execution tests
493
440
494
441
In execution tests, memory management testing should be performed
495
442
using local variables enclosed in a closure passed to the standard
496
443
library `` autoreleasepool `` function. For example::
497
444
445
+ ```
498
446
// A counter that's decremented by Canary's deinitializer.
499
447
var CanaryCount = 0
500
448
@@ -510,6 +458,7 @@ library ``autoreleasepool`` function. For example::
510
458
let canary = Canary()
511
459
}
512
460
assert(CanaryCount == 1, "canary was not released")
461
+ ```
513
462
514
463
Memory management tests should be performed in a local scope because Swift does
515
464
not guarantee the destruction of global variables. Code that needs to
0 commit comments