| ... | ... | @@ -1173,74 +1173,6 @@ fn addOne(number: i32) i32 { |
| 1173 | 1173 | </p> |
| 1174 | 1174 | {#see_also|The Global Error Set|Grammar#} |
| 1175 | 1175 | {#header_close#} |
| 1176 | | {#header_open|Nested Container Tests#} |
| 1177 | | <p> |
| 1178 | | When the <kbd>zig test</kbd> tool is building a test runner, only resolved {#syntax#}test{#endsyntax#} |
| 1179 | | declarations are included in the build. Initially, only the given Zig source file's top-level |
| 1180 | | declarations are resolved. Unless nested {#link|containers|Containers#} are referenced from a top-level test declaration, |
| 1181 | | nested container tests will not be resolved. |
| 1182 | | </p> |
| 1183 | | <p> |
| 1184 | | The code sample below uses the {#syntax#}std.testing.refAllDecls(@This()){#endsyntax#} function call to |
| 1185 | | reference all of the containers that are in the file including the imported Zig source file. The code |
| 1186 | | sample also shows an alternative way to reference containers using the {#syntax#}_ = C;{#endsyntax#} |
| 1187 | | syntax. This syntax tells the compiler to ignore the result of the expression on the right side of the |
| 1188 | | assignment operator. |
| 1189 | | </p> |
| 1190 | | {#code_begin|test|testing_nested_container_tests#} |
| 1191 | | const std = @import("std"); |
| 1192 | | const expect = std.testing.expect; |
| 1193 | | |
| 1194 | | // Imported source file tests will run when referenced from a top-level test declaration. |
| 1195 | | // The next line alone does not cause "testing_introduction.zig" tests to run. |
| 1196 | | const imported_file = @import("testing_introduction.zig"); |
| 1197 | | |
| 1198 | | test { |
| 1199 | | // To run nested container tests, either, call `refAllDecls` which will |
| 1200 | | // reference all declarations located in the given argument. |
| 1201 | | // `@This()` is a builtin function that returns the innermost container it is called from. |
| 1202 | | // In this example, the innermost container is this file (implicitly a struct). |
| 1203 | | std.testing.refAllDecls(@This()); |
| 1204 | | |
| 1205 | | // or, reference each container individually from a top-level test declaration. |
| 1206 | | // The `_ = C;` syntax is a no-op reference to the identifier `C`. |
| 1207 | | _ = S; |
| 1208 | | _ = U; |
| 1209 | | _ = @import("testing_introduction.zig"); |
| 1210 | | } |
| 1211 | | |
| 1212 | | const S = struct { |
| 1213 | | test "S demo test" { |
| 1214 | | try expect(true); |
| 1215 | | } |
| 1216 | | |
| 1217 | | const SE = enum { |
| 1218 | | V, |
| 1219 | | |
| 1220 | | // This test won't run because its container (SE) is not referenced. |
| 1221 | | test "This Test Won't Run" { |
| 1222 | | try expect(false); |
| 1223 | | } |
| 1224 | | }; |
| 1225 | | }; |
| 1226 | | |
| 1227 | | const U = union { // U is referenced by the file's top-level test declaration |
| 1228 | | s: US, // and US is referenced here; therefore, "U.Us demo test" will run |
| 1229 | | |
| 1230 | | const US = struct { |
| 1231 | | test "U.US demo test" { |
| 1232 | | // This test is a top-level test declaration for the struct. |
| 1233 | | // The struct is nested (declared) inside of a union. |
| 1234 | | try expect(true); |
| 1235 | | } |
| 1236 | | }; |
| 1237 | | |
| 1238 | | test "U demo test" { |
| 1239 | | try expect(true); |
| 1240 | | } |
| 1241 | | }; |
| 1242 | | {#code_end#} |
| 1243 | | {#header_close#} |
| 1244 | 1176 | {#header_open|Test Failure#} |
| 1245 | 1177 | <p> |
| 1246 | 1178 | The default test runner checks for an {#link|error|Errors#} returned from a test. |