Skip to content

Add --list-test-cases and --run-test-case #378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- Changed error message for too long Unix domain socket paths (gh-341).
- Add `cbuilder` helper as a declarative configuration builder (gh-366).
- Make `assert_error_*` additionally check error trace if required.
- Add `--list-test-cases` and `--run-test-case` CLI options.

## 1.0.1

Expand Down
29 changes: 26 additions & 3 deletions luatest/runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ Options:
--coverage: Use luacov to collect code coverage.
--no-clean: Disable the var directory (default: /tmp/t) deletion before
running tests.
--list-test-cases List all test cases.
--run-test-case CASE Run one specific test case. Unlike --pattern the name
should match verbatim.
]]

function Runner.parse_cmd_line(args)
Expand Down Expand Up @@ -236,6 +239,10 @@ function Runner.parse_cmd_line(args)
result.coverage_report = true
elseif arg == '--no-clean' then
result.no_clean = true
elseif arg == '--list-test-cases' then
result.list_test_cases = true
elseif arg == '--run-test-case' then
result.run_test_case = next_arg()
elseif arg:sub(1,1) == '-' then
error('Unknown option: ' .. arg)
elseif arg:find('/') then
Expand Down Expand Up @@ -284,10 +291,16 @@ function Runner.is_test_name(s)
return string.sub(s, 1, 4):lower() == 'test'
end

function Runner.filter_tests(tests, patterns)
function Runner.filter_tests(tests, patterns, test_case)
local result = {[true] = {}, [false] = {}}
for _, test in ipairs(tests) do
table.insert(result[utils.pattern_filter(patterns, test.name)], test)
-- Handle --pattern CLI option.
local yesno = utils.pattern_filter(patterns, test.name)
-- Handle --run-test-case CLI option.
if test_case ~= nil then
yesno = yesno and test.name == test_case
end
table.insert(result[yesno], test)
end
return result
end
Expand Down Expand Up @@ -349,7 +362,17 @@ end

function Runner.mt:run()
self:bootstrap()
local filtered_list = self.class.filter_tests(self:find_tests(), self.tests_pattern)
local filtered_list = self.class.filter_tests(self:find_tests(),
self.tests_pattern, self.run_test_case)

-- Handle the --list-test-case CLI option.
if self.list_test_cases then
for _, test_case in ipairs(filtered_list[true]) do
print(test_case.name)
end
return 0
end

self:start_suite(#filtered_list[true], #filtered_list[false])
self:cleanup()
self:run_tests(filtered_list[true])
Expand Down
21 changes: 21 additions & 0 deletions test/luaunit/utility_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,12 @@ function g.test_parse_cmd_line()
output_file_name='toto.xml',
})

-- list-test-cases
assert_subject({'--list-test-cases'}, {list_test_cases = true})

-- run-test-case
assert_subject({'--run-test-case', 'foo'}, {run_test_case = 'foo'})

t.assert_error_msg_contains('option: -$', subject, {'-$',})
end

Expand Down Expand Up @@ -708,6 +714,21 @@ function g.test_filter_tests()
included, excluded = subject(testset, {'foo', 'bar', '!t.t.', '%.bar'})
t.assert_equals(included, {testset[2], testset[4], testset[6], testset[7], testset[8]})
t.assert_equals(#excluded, 3)

-- --run-test-case without patterns
included, excluded = subject(testset, nil, 'toto.foo')
t.assert_equals(included, {testset[1]})
t.assert_equals(#excluded, 7)

-- --run-test-case with a matching pattern
included, excluded = subject(testset, {'toto'}, 'toto.foo')
t.assert_equals(included, {testset[1]})
t.assert_equals(#excluded, 7)

-- --run-test-case with a non-matching pattern
included, excluded = subject(testset, {'tutu'}, 'toto.foo')
t.assert_equals(included, {})
t.assert_equals(#excluded, 8)
end

function g.test_str_match()
Expand Down
Loading