在 Python 中生成 py.test 测试 [英] Generating py.test tests in Python

查看:55
本文介绍了在 Python 中生成 py.test 测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先提问,感兴趣再解释.

Question first, then an explanation if you're interested.

在 py.test 的上下文中,如何从一小组测试函数模板生成大量测试函数?

In the context of py.test, how do I generate a large set of test functions from a small set of test-function templates?

类似于:

models = [model1,model2,model3]
data_sets = [data1,data2,data3]

def generate_test_learn_parameter_function(model,data):
    def this_test(model,data):
        param = model.learn_parameters(data)
        assert((param - model.param) < 0.1 )
    return this_test

for model,data in zip(models,data_sets):
    # how can py.test can see the results of this function?
    generate_test_learn_parameter_function(model,data)

说明:

我正在编写的代码采用模型结构、一些数据,并学习模型的参数.所以我的单元测试包括一堆模型结构和预先生成的数据集,然后在每个结构+数据上完成一组大约 5 个机器学习任务.

The code I'm writing takes a model structure, some data, and learns the parameters of the model. So my unit testing consists of a bunch of model structures and pre-generated data sets, and then a set of about 5 machine learning tasks to complete on each structure+data.

因此,如果我手动编写代码,我需要对每个任务每个模型进行一次测试.每次我想出一个新模型时,我都需要复制并粘贴 5 个任务,更改我指向的腌制结构 + 数据.这对我来说感觉像是不好的做法.理想情况下,我想要的是 5 个模板函数,用于定义我的 5 个任务中的每一个,然后为我指定的结构列表吐出测试函数.

So if I hand code this I need one test per model per task. Every time I come up with a new model I need to then copy and paste the 5 tasks, changing which pickled structure+data I'm pointing at. This feels like bad practice to me. Ideally what I'd like is 5 template functions that define each of my 5 tasks and then to just spit out test functions for a list of structures that I specify.

谷歌搜索将我带到 a) 工厂或 b) 闭包,这两者都让我感到困惑,并建议我必须有一种更简单的方法,因为这个问题必须由合适的程序员定期面对.那么有吗?

Googling about brings me to either a) factories or b) closures, both of which addle my brain and suggest to me that there must be an easier way, as this problem must be faced regularly by proper programmers. So is there?

这里是如何解决这个问题!

So here's how to solve this problem!

def pytest_generate_tests(metafunc):
    if "model" in metafunc.funcargnames:
        models = [model1,model2,model3]
        for model in models:
            metafunc.addcall(funcargs=dict(model=model))

def test_awesome(model):
    assert model == "awesome"

这会将 test_awesome 测试应用到我的模型列表中的每个模型!谢谢@dfichter!

This will apply the test_awesome test to each model in my list of models! Thanks @dfichter!

(注意:断言总是通过,顺便说一句)

(NOTE: that assert always passes, btw)

推荐答案

良好的直觉.py.test 通过它的 pytest_generate_tests() 钩子完全支持你所说的.他们在此处进行了解释.

Good instincts. py.test supports exactly what you're talking about with its pytest_generate_tests() hook. They explain it here.

这篇关于在 Python 中生成 py.test 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆