Skip to content

Commit fcabd2a

Browse files
Totktonadaylobankov
authored andcommitted
Add --list-test-cases and --run-test-case
These options allow an external runner (such as [test-run][test-run]) to run the test cases separately: isolated and/or parallel. The idea is the following. * test-run has some criteria, whether to parallelize the given test. * If it needs to be parallelized, test-run invokes `luatest --list-test-cases <..path to test..>`. * Then each test case is run in its own process using `luatest --run-test-case <..test case..> <..path to test..>`. The `--run-test-case` option may be simulated using `--pattern`, but it requires regexp escaping. It is more convenient to have a separate option to pass a verbatim test case name. The new CLI options are mostly for scripting and not for interactive use. So, no short option are added for them. [test-run] https://github.com/tarantool/test-run
1 parent dfee2f3 commit fcabd2a

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- Changed error message for too long Unix domain socket paths (gh-341).
1212
- Add `cbuilder` helper as a declarative configuration builder (gh-366).
1313
- Make `assert_error_*` additionally check error trace if required.
14+
- Add `--list-test-cases` and `--run-test-case` CLI options.
1415

1516
## 1.0.1
1617

luatest/runner.lua

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ Options:
161161
--coverage: Use luacov to collect code coverage.
162162
--no-clean: Disable the var directory (default: /tmp/t) deletion before
163163
running tests.
164+
--list-test-cases List all test cases.
165+
--run-test-case CASE Run one specific test case. Unlike --pattern the name
166+
should match verbatim.
164167
]]
165168

166169
function Runner.parse_cmd_line(args)
@@ -236,6 +239,10 @@ function Runner.parse_cmd_line(args)
236239
result.coverage_report = true
237240
elseif arg == '--no-clean' then
238241
result.no_clean = true
242+
elseif arg == '--list-test-cases' then
243+
result.list_test_cases = true
244+
elseif arg == '--run-test-case' then
245+
result.run_test_case = next_arg()
239246
elseif arg:sub(1,1) == '-' then
240247
error('Unknown option: ' .. arg)
241248
elseif arg:find('/') then
@@ -284,10 +291,16 @@ function Runner.is_test_name(s)
284291
return string.sub(s, 1, 4):lower() == 'test'
285292
end
286293

287-
function Runner.filter_tests(tests, patterns)
294+
function Runner.filter_tests(tests, patterns, test_case)
288295
local result = {[true] = {}, [false] = {}}
289296
for _, test in ipairs(tests) do
290-
table.insert(result[utils.pattern_filter(patterns, test.name)], test)
297+
-- Handle --pattern CLI option.
298+
local yesno = utils.pattern_filter(patterns, test.name)
299+
-- Handle --run-test-case CLI option.
300+
if test_case ~= nil then
301+
yesno = yesno and test.name == test_case
302+
end
303+
table.insert(result[yesno], test)
291304
end
292305
return result
293306
end
@@ -349,7 +362,17 @@ end
349362

350363
function Runner.mt:run()
351364
self:bootstrap()
352-
local filtered_list = self.class.filter_tests(self:find_tests(), self.tests_pattern)
365+
local filtered_list = self.class.filter_tests(self:find_tests(),
366+
self.tests_pattern, self.run_test_case)
367+
368+
-- Handle the --list-test-case CLI option.
369+
if self.list_test_cases then
370+
for _, test_case in ipairs(filtered_list[true]) do
371+
print(test_case.name)
372+
end
373+
return 0
374+
end
375+
353376
self:start_suite(#filtered_list[true], #filtered_list[false])
354377
self:cleanup()
355378
self:run_tests(filtered_list[true])

test/luaunit/utility_test.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,12 @@ function g.test_parse_cmd_line()
610610
output_file_name='toto.xml',
611611
})
612612

613+
-- list-test-cases
614+
assert_subject({'--list-test-cases'}, {list_test_cases = true})
615+
616+
-- run-test-case
617+
assert_subject({'--run-test-case', 'foo'}, {run_test_case = 'foo'})
618+
613619
t.assert_error_msg_contains('option: -$', subject, {'-$',})
614620
end
615621

@@ -708,6 +714,21 @@ function g.test_filter_tests()
708714
included, excluded = subject(testset, {'foo', 'bar', '!t.t.', '%.bar'})
709715
t.assert_equals(included, {testset[2], testset[4], testset[6], testset[7], testset[8]})
710716
t.assert_equals(#excluded, 3)
717+
718+
-- --run-test-case without patterns
719+
included, excluded = subject(testset, nil, 'toto.foo')
720+
t.assert_equals(included, {testset[1]})
721+
t.assert_equals(#excluded, 7)
722+
723+
-- --run-test-case with a matching pattern
724+
included, excluded = subject(testset, {'toto'}, 'toto.foo')
725+
t.assert_equals(included, {testset[1]})
726+
t.assert_equals(#excluded, 7)
727+
728+
-- --run-test-case with a non-matching pattern
729+
included, excluded = subject(testset, {'tutu'}, 'toto.foo')
730+
t.assert_equals(included, {})
731+
t.assert_equals(#excluded, 8)
711732
end
712733

713734
function g.test_str_match()

0 commit comments

Comments
 (0)