luatest
luatest copied to clipboard
Add some kind of fixture which will be work in context of file
I often use some groups in one file. For example, some kind of API tests, where each group contains tests that check API endpoint or method in different ways. But preparation for each of these groups is similar. So would be great if some fixture will prepare an environment in the context of the entire file, and not for each group.
I suppose it may be suppressed by https://github.com/tarantool/luatest/issues/116
Looks like you can use before_suite
and after_suite
hooks. For example:
-- some_test.lua
local t = require('luatest')
local g1 = t.group('g1')
local g2 = t.group('g2')
t.before_suite(function()
print('before_suite')
end)
t.after_suite(function()
print('after_suite')
end)
g1.before_all(function()
print('g1: before_all')
end)
g1.after_all(function()
print('g1: after_all')
end)
g1.before_each(function()
print('g1: before_each')
end)
g1.after_each(function()
print('g1: after_each')
end)
g1.test1_g1 = function()
t.assert_equals(1, 1)
end
g1.test2_g1 = function()
t.assert_equals(1, 1)
end
g2.before_all(function()
print('g2: before_all')
end)
g2.after_all(function()
print('g2: after_all')
end)
g2.before_each(function()
print('g2: before_each')
end)
g2.after_each(function()
print('g2: after_each')
end)
g2.test1_g2 = function()
t.assert_equals(1, 1)
end
g2.test2_g2 = function()
t.assert_equals(1, 1)
end
$ ./bin/luatest -o nil -c
Tarantool version is 2.10.4-0-g816000e
before_suite
g1: before_all
g1: before_each
g1: after_each
g1: before_each
g1: after_each
g1: after_all
g2: before_all
g2: before_each
g2: after_each
g2: before_each
g2: after_each
g2: after_all
after_suite
No, I can't, 'cause I want run before and after handlers on each file, not on entire suite.
No, I can't, 'cause I want run before and after handlers on each file, not on entire suite.
Could you please provide a test example?
Yep.
file1.lua
local t = require('luatest')
t.before_file(function()
end)
t.after_file(function()
end)
local g1 = t.group('g1')
g1.before_all(function()
print('g1: before_all')
end)
g1.after_all(function()
print('g1: after_all')
end)
g1.before_each(function()
print('g1: before_each')
end)
g1.after_each(function()
print('g1: after_each')
end)
g1.test_g1 = function()
t.assert_equals(1, 1)
end
local g2 = t.group('g2')
g2.before_all(function()
print('g1: before_all')
end)
g2.after_all(function()
print('g1: after_all')
end)
g2.before_each(function()
print('g1: before_each')
end)
g2.after_each(function()
print('g1: after_each')
end)
g2.test_g2 = function()
t.assert_equals(1, 1)
end
file2.lua
local t = require('luatest')
t.before_file(function()
end)
t.after_file(function()
end)
local g3 = t.group('g3')
g3.before_all(function()
print('g1: before_all')
end)
g3.after_all(function()
print('g1: after_all')
end)
g3.before_each(function()
print('g1: before_each')
end)
g3.after_each(function()
print('g1: after_each')
end)
g3.test_g3 = function()
t.assert_equals(1, 1)
end
local g4 = t.group('g4')
g4.before_all(function()
print('g1: before_all')
end)
g4.after_all(function()
print('g1: after_all')
end)
g4.before_each(function()
print('g1: before_each')
end)
g4.after_each(function()
print('g1: after_each')
end)
g4.test1_g4 = function()
t.assert_equals(1, 1)
end
Something like that.
Yep.
file1.lua
local t = require('luatest') t.before_file(function() end) t.after_file(function() end) local g1 = t.group('g1') g1.before_all(function() print('g1: before_all') end) g1.after_all(function() print('g1: after_all') end) g1.before_each(function() print('g1: before_each') end) g1.after_each(function() print('g1: after_each') end) g1.test_g1 = function() t.assert_equals(1, 1) end local g2 = t.group('g2') g2.before_all(function() print('g1: before_all') end) g2.after_all(function() print('g1: after_all') end) g2.before_each(function() print('g1: before_each') end) g2.after_each(function() print('g1: after_each') end) g2.test_g2 = function() t.assert_equals(1, 1) end
file2.lua
local t = require('luatest') t.before_file(function() end) t.after_file(function() end) local g3 = t.group('g3') g3.before_all(function() print('g1: before_all') end) g3.after_all(function() print('g1: after_all') end) g3.before_each(function() print('g1: before_each') end) g3.after_each(function() print('g1: after_each') end) g3.test_g3 = function() t.assert_equals(1, 1) end local g4 = t.group('g4') g4.before_all(function() print('g1: before_all') end) g4.after_all(function() print('g1: after_all') end) g4.before_each(function() print('g1: before_each') end) g4.after_each(function() print('g1: after_each') end) g4.test1_g4 = function() t.assert_equals(1, 1) end
Something like that.
Sorry, maybe I am missing something, but before_suite/after_suite
fixtures work exactly as you expect it from before_file/after_file
hooks in your snippet. Please take a look at my example above more carefully, you will see it.
$ ./bin/luatest -o nil -c
Tarantool version is 2.10.4-0-g816000e
before_suite
...
...
...
after_suite
I want to exec different prepare scenarios on the each file.
If I run all suite, then I expect next output:
before_suite
before_file1
g1.before_all
...
g1.after_all
g2.before_all
...
g2.after_all
after_file1
before_file2
g3.before_all
...
g3.after_all
g4.before_all
...
g4.after_all
after_file2
after_suite
I want to exec different prepare scenarios on the each file.
If I run all suite, then I expect next output:
before_suite before_file1 g1.before_all ... g1.after_all g2.before_all ... g2.after_all after_file1 before_file2 g3.before_all ... g3.after_all g4.before_all ... g4.after_all after_file2 after_suite
Actually not. As far as I know, in terms of luatest a test suite is one test file, not a folder with a number of files. Probably, we have broken naming here and before_file/after_file
or before_module/after_module
would be better names.
As far as I know, in terms of luatest a test suite is one test file, not a folder with a number of files.
It doesn't work that way. The actual output is the following:
$ luatest -v -c
Tarantool version is 2.11.0-entrypoint-671-gdec0e0221
Started on Wed Jan 18 2023
(g3+g4): before_suite
(g1+g2): before_suite
g1.test_g1 ...
g1: test
g1.test_g1 ... (0.000s) Ok
g2.test_g2 ...
g2: test
g2.test_g2 ... (0.000s) Ok
g3.test_g3 ...
g3: test
g3.test_g3 ... (0.000s) Ok
g4.test_g4 ...
g4: test
g4.test_g4 ... (0.000s) Ok
(g3+g4): after_suite
(g1+g2): after_suite
=========================================================
Ran 4 tests in 0.001 seconds, 4 succeeded, 0 failed
Probably, we have broken naming here
You didn't. Not a single line in readme / documentation gives an explanation of what "a suite" is. In fact a suite is a union of all tests visible to luatest, not a file.
Proof
test/g12_test.lua
local t = require('luatest')
t.before_suite(function()
print('(g1+g2): before_suite')
end)
t.after_suite(function()
print('(g1+g2): after_suite')
end)
local g1 = t.group('g1')
g1.test_g1 = function()
print('g1: test')
t.assert_equals(1, 1)
end
local g2 = t.group('g2')
g2.test_g2 = function()
print('g2: test')
t.assert_equals(1, 1)
end
test/g34_test.lua
local t = require('luatest')
t.before_suite(function()
print('(g3+g4): before_suite')
end)
t.after_suite(function()
print('(g3+g4): after_suite')
end)
local g3 = t.group('g3')
g3.test_g3 = function()
print('g3: test')
t.assert_equals(1, 1)
end
local g4 = t.group('g4')
g4.test_g4 = function()
print('g4: test')
t.assert_equals(1, 1)
end
@Kasen Thank you for your explanations and patience :) @rosik Thank you for your explanation!
Reopened.