luatest icon indicating copy to clipboard operation
luatest copied to clipboard

Add some kind of fixture which will be work in context of file

Open Kasen opened this issue 3 years ago • 10 comments

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.

Kasen avatar Aug 16 '21 11:08 Kasen

I suppose it may be suppressed by https://github.com/tarantool/luatest/issues/116

rosik avatar Sep 03 '21 09:09 rosik

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

ylobankov avatar Jan 11 '23 12:01 ylobankov

No, I can't, 'cause I want run before and after handlers on each file, not on entire suite.

Kasen avatar Jan 12 '23 10:01 Kasen

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?

ylobankov avatar Jan 12 '23 11:01 ylobankov

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.

Kasen avatar Jan 16 '23 07:01 Kasen

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

ylobankov avatar Jan 17 '23 16:01 ylobankov

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

Kasen avatar Jan 17 '23 16:01 Kasen

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.

ylobankov avatar Jan 17 '23 16:01 ylobankov

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

rosik avatar Jan 17 '23 23:01 rosik

@Kasen Thank you for your explanations and patience :) @rosik Thank you for your explanation!

Reopened.

ylobankov avatar Jan 18 '23 14:01 ylobankov