-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_runner.zig
More file actions
324 lines (273 loc) · 11.1 KB
/
test_runner.zig
File metadata and controls
324 lines (273 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
const std = @import("std");
const suite = @import("suite.zig");
const reporter_mod = @import("reporter.zig");
const parallel = @import("parallel.zig");
const compat = @import("compat.zig");
pub const RunnerError = error{
NoTestsFound,
AllTestsFailed,
};
pub const RunnerOptions = struct {
bail: bool = false, // Stop on first failure
filter: ?[]const u8 = null, // Test name filter
reporter_type: ReporterType = .spec,
use_colors: bool = true,
parallel: bool = false, // Enable parallel execution
n_jobs: ?usize = null, // Number of parallel jobs
};
pub const ReporterType = enum {
spec,
dot,
json,
tap,
junit,
};
pub const TestRunner = struct {
allocator: std.mem.Allocator,
registry: *suite.TestRegistry,
options: RunnerOptions,
results: reporter_mod.TestResults,
const Self = @This();
pub fn init(allocator: std.mem.Allocator, registry: *suite.TestRegistry, options: RunnerOptions) Self {
return Self{
.allocator = allocator,
.registry = registry,
.options = options,
.results = reporter_mod.TestResults.init(allocator),
};
}
pub fn deinit(self: *Self) void {
self.results.deinit();
}
/// Run all registered tests
pub fn run(self: *Self) !bool {
const stdout_file = std.Io.File.stdout();
var stdout_buffer: [4096]u8 = undefined;
var threaded_io: std.Io.Threaded = .init(std.mem.Allocator.failing, .{ .environ = .empty });
defer threaded_io.deinit();
var stdout_writer = stdout_file.writer(threaded_io.io(), &stdout_buffer);
// Create reporter
var spec_reporter = reporter_mod.SpecReporter.init(self.allocator, stdout_writer.interface);
var dot_reporter = reporter_mod.DotReporter.init(self.allocator, stdout_writer.interface);
var json_reporter = reporter_mod.JsonReporter.init(self.allocator, stdout_writer.interface);
defer json_reporter.deinit();
var tap_reporter = reporter_mod.TAPReporter.init(self.allocator, stdout_writer.interface);
var junit_reporter = reporter_mod.JUnitReporter.init(self.allocator, "test-results.xml");
defer junit_reporter.deinit();
var current_reporter: *reporter_mod.Reporter = switch (self.options.reporter_type) {
.spec => &spec_reporter.reporter,
.dot => &dot_reporter.reporter,
.json => &json_reporter.reporter,
.tap => &tap_reporter.reporter,
.junit => &junit_reporter.reporter,
};
current_reporter.use_colors = self.options.use_colors;
const total_tests = self.registry.countAllTests();
if (total_tests == 0) {
return RunnerError.NoTestsFound;
}
// Notify reporter of run start
try current_reporter.onRunStart(total_tests);
// Run tests in parallel or sequential based on options
if (self.options.parallel) {
const parallel_opts = parallel.ParallelOptions{
.enabled = true,
.n_jobs = self.options.n_jobs,
};
_ = parallel.runTestsParallel(
self.allocator,
self.registry,
current_reporter,
parallel_opts,
) catch |err| {
std.debug.print("Parallel execution failed: {any}, falling back to sequential\n", .{err});
// Fall back to sequential execution
for (self.registry.root_suites.items) |test_suite| {
try self.runSuite(test_suite, current_reporter);
if (self.options.bail and self.results.failed > 0) {
break;
}
}
return false;
};
// Update results from parallel execution
self.results.total = 0;
self.results.passed = 0;
self.results.failed = 0;
self.results.skipped = 0;
for (self.registry.root_suites.items) |test_suite| {
for (test_suite.tests.items) |test_case| {
self.results.total += 1;
switch (test_case.status) {
.passed => self.results.passed += 1,
.failed => self.results.failed += 1,
.skipped => self.results.skipped += 1,
else => {},
}
}
}
} else {
// Sequential execution (original behavior)
for (self.registry.root_suites.items) |test_suite| {
try self.runSuite(test_suite, current_reporter);
if (self.options.bail and self.results.failed > 0) {
break;
}
}
}
// Notify reporter of run end
try current_reporter.onRunEnd(&self.results);
// Flush output
try stdout_writer.interface.flush();
return self.results.failed == 0;
}
/// Run a single test suite
fn runSuite(self: *Self, test_suite: *suite.TestSuite, rep: *reporter_mod.Reporter) !void {
// Skip if marked as skip or if has_only and this isn't marked as only
if (test_suite.shouldSkip()) {
try self.skipAllTests(test_suite);
return;
}
if (self.registry.has_only and !test_suite.hasOnly()) {
try self.skipAllTests(test_suite);
return;
}
// Notify reporter
try rep.onSuiteStart(test_suite.name);
// Run beforeAll hooks
test_suite.runBeforeAllHooks(self.allocator) catch |err| {
std.debug.print("beforeAll hook failed: {any}\n", .{err});
try self.skipAllTests(test_suite);
try rep.onSuiteEnd(test_suite.name);
return;
};
// Run tests in this suite
for (test_suite.tests.items) |*test_case| {
if (test_case.skip or (self.registry.has_only and !test_case.only)) {
test_case.status = .skipped;
try rep.onTestEnd(test_case);
try self.results.addTest(test_case);
continue;
}
// Check filter
if (self.options.filter) |filter| {
if (std.mem.indexOf(u8, test_case.name, filter) == null) {
test_case.status = .skipped;
try rep.onTestEnd(test_case);
try self.results.addTest(test_case);
continue;
}
}
try self.runTest(test_case, test_suite, rep);
if (self.options.bail and test_case.status == .failed) {
break;
}
}
// Run nested suites
for (test_suite.suites.items) |nested_suite| {
try self.runSuite(nested_suite, rep);
if (self.options.bail and self.results.failed > 0) {
break;
}
}
// Run afterAll hooks
test_suite.runAfterAllHooks(self.allocator) catch |err| {
std.debug.print("afterAll hook failed: {any}\n", .{err});
};
try rep.onSuiteEnd(test_suite.name);
}
/// Run a single test
fn runTest(self: *Self, test_case: *suite.TestCase, test_suite: *suite.TestSuite, rep: *reporter_mod.Reporter) !void {
try rep.onTestStart(test_case.name);
test_case.status = .running;
const start_time = compat.nanoTimestamp();
// Get all beforeEach hooks (including parent hooks)
var before_hooks = try test_suite.getAllBeforeEachHooks(self.allocator);
defer before_hooks.deinit(self.allocator);
// Run beforeEach hooks
var before_failed = false;
for (before_hooks.items) |hook| {
hook(self.allocator) catch |err| {
test_case.status = .failed;
const err_msg = try std.fmt.allocPrint(self.allocator, "beforeEach hook failed: {any}", .{err});
test_case.error_message = err_msg;
before_failed = true;
break;
};
}
// Run the actual test if beforeEach succeeded
if (!before_failed) {
test_case.test_fn(self.allocator) catch |err| {
test_case.status = .failed;
const err_msg = try std.fmt.allocPrint(self.allocator, "{any}", .{err});
test_case.error_message = err_msg;
};
if (test_case.status == .running) {
test_case.status = .passed;
}
}
// Get all afterEach hooks (including parent hooks)
var after_hooks = try test_suite.getAllAfterEachHooks(self.allocator);
defer after_hooks.deinit(self.allocator);
// Run afterEach hooks (always run, even if test failed)
for (after_hooks.items) |hook| {
hook(self.allocator) catch |err| {
std.debug.print("afterEach hook failed: {any}\n", .{err});
};
}
const end_time = compat.nanoTimestamp();
test_case.execution_time_ns = @intCast(end_time - start_time);
try self.results.addTest(test_case);
try rep.onTestEnd(test_case);
}
/// Skip all tests in a suite
fn skipAllTests(self: *Self, test_suite: *suite.TestSuite) !void {
for (test_suite.tests.items) |*test_case| {
test_case.status = .skipped;
try self.results.addTest(test_case);
}
for (test_suite.suites.items) |nested_suite| {
try self.skipAllTests(nested_suite);
}
}
};
/// Helper function to run tests with default options
pub fn runTests(allocator: std.mem.Allocator, registry: *suite.TestRegistry) !bool {
var runner = TestRunner.init(allocator, registry, .{});
defer runner.deinit();
return try runner.run();
}
/// Helper function to run tests with custom options
pub fn runTestsWithOptions(allocator: std.mem.Allocator, registry: *suite.TestRegistry, options: RunnerOptions) !bool {
var runner = TestRunner.init(allocator, registry, options);
defer runner.deinit();
return try runner.run();
}
// Tests
test "ReporterType enum values" {
const spec = ReporterType.spec;
const dot = ReporterType.dot;
const json = ReporterType.json;
try std.testing.expect(spec == .spec);
try std.testing.expect(dot == .dot);
try std.testing.expect(json == .json);
}
test "RunnerOptions default values" {
const options = RunnerOptions{};
try std.testing.expectEqual(false, options.bail);
try std.testing.expectEqual(@as(?[]const u8, null), options.filter);
try std.testing.expectEqual(ReporterType.spec, options.reporter_type);
try std.testing.expectEqual(true, options.use_colors);
}
test "RunnerOptions custom values" {
const options = RunnerOptions{
.bail = true,
.filter = "test",
.reporter_type = .json,
.use_colors = false,
};
try std.testing.expectEqual(true, options.bail);
try std.testing.expectEqualStrings("test", options.filter.?);
try std.testing.expectEqual(ReporterType.json, options.reporter_type);
try std.testing.expectEqual(false, options.use_colors);
}