如何获取pytest装饰的测试函数名称和参数在pytest的装置中。

huangapple go评论64阅读模式
英文:

How to retrive decorated test function name and arguments in a pytest fixture

问题

以下是翻译好的部分:

我想要手动运行装饰的测试用例,它的装置中,我可以这样做吗?
例如:

@pytest.fixture
def my_fixture(request):
    调用 test_my_function
    再次调用 test_my_function

def test_my_function(my_fixture):
    pass

用户案例如下:
我们想要创建一个测试框架,其中有设置(setup)和拆卸(teardown)阶段,以及一个特殊的目标:我们想在设置(setup)和拆卸(teardown)之间运行测试两次。这是因为我们有一个共同的刷新函数,它将重建测试的数据库,我们想要确认刷新后一切都没有改变。因此,在刷新函数调用之前和之后应该运行两次测试。

我想知道如何获取函数名称和参数,然后手动运行它。

英文:

I want to run the decorated test manually in its fixture, can I do that?
for example:

@pytest.fixture
def my_fixture(request):
    call test_my_function
    call test_my_fucntion again

def test_my_function(my_fixture):
    pass

The user-case is as below:
We want to make a test framework, in which we have setup and teardown phase, and a special target: we want to run the test twice times between setup and teardown. That's because we have a common refreash function, it will reconstruct the database of the test, we want to confirm everything are unchanged after refresh. So there should be two times test before and after the refresh function call.

I want to know how to get function name and arguments, and run it manually

答案1

得分: 1

import inspect
argspec = inspect.getfullargspec(test_my_function)
print(argspec.args)

输出:

['my_fixture']

或者只需:

print(test_my_function.code.co_varnames)

输出:

('my_fixture',)

要获取函数的__name__

print(my_fixture.name)
print(test_my_function.name)

输出:

my_fixture
test_my_function

如果要获取pytest装饰的my_fixture函数的args__name__,可以使用@pytest.mark.parametrize

@pytest.mark.parametrize
def my_fixture(request):
pass

def test_my_function(my_fixture):
pass

test_my_function(my_fixture)
print(my_fixture.code.co_varnames)
print(my_fixture.name)

输出:

('request',)
my_fixture

要继续使用pytest.fixture装饰器,可以将pytest.mark.parametrize属性添加到它:

pytest.fixture.parametrize = pytest.mark.parametrize

@pytest.fixture.parametrize
def my_fixture(request):
pass

def test_my_function(my_fixture):
pass

test_my_function(my_fixture)
print(my_fixture.code.co_varnames)
print(my_fixture.name)

输出:

('request',)
my_fixture

英文:

We can use getfullargspec (from import inspect):

import inspect
argspec = inspect.getfullargspec(test_my_function)
print(argspec.args)

Outputs:

['my_fixture']

or just:

print(test_my_function.__code__.co_varnames)

Outputs:

('my_fixture',)

To get the __name__s of the functions:

print(my_fixture.__name__)
print(test_my_function.__name__)

Outputs:

my_fixture
test_my_function

If you want to get the args and __name__ of the pytest decorated function my_fixture we can use @pytest.mark.parametrize:

@pytest.mark.parametrize
def my_fixture(request):
    pass

def test_my_function(my_fixture):
    pass

test_my_function(my_fixture)
print(my_fixture.__code__.co_varnames)
print(my_fixture.__name__)

Outputs:

('request',)
my_fixture

To continue using the pytest.fixture decorator we can add a parametrize attribute pytest.mark.parametrize to it:

pytest.fixture.parametrize = pytest.mark.parametrize

@pytest.fixture.parametrize
def my_fixture(request):
    pass

def test_my_function(my_fixture):
    pass

test_my_function(my_fixture)
print(my_fixture.__code__.co_varnames)
print(my_fixture.__name__)

Outputs:

('request',)
my_fixture

huangapple
  • 本文由 发表于 2023年4月11日 09:41:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75981846.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定