单元测试与 pytest [英] unittest Vs pytest

查看:53
本文介绍了单元测试与 pytest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在单元测试中,我可以在一个类中设置变量,然后这个类的方法可以选择它想要使用的任何变量...

In unittest, I can setUp variables in a class and then the methods of this class can chose whichever variable it wants to use...

class test_class(unittest.TestCase):
    def setUp(self):        
        self.varA = 1
        self.varB = 2
        self.varC = 3
        self.modified_varA = 2

    def test_1(self):
        do_something_with_self.varA, self.varB

    def test_2(self):
        do_something_with_self_modified_varA, self.varC

所以在 unittest 中,很容易将一堆测试放在一起,这些测试可以放在一个类下,然后对不同的方法使用许多不同的变量(varA 和 varB).在pytest中,我在conftest.py中创建了一个fixture,而不是unittest中的一个类,就像这样...

So in unittest, it was easy to put bunch of tests together that could go under one class and then use many different variables (varA and varB) for different methods. In pytest, I created a fixture in conftest.py instead of a class in unittest, like this...

@pytest.fixture(scope="module")
def input1():
    varA = 1
    varB = 2
    return varA, varB

@pytest.fixture(scope="module")
def input2():
    varA = 2
    varC = 3
    return varA, varC

我将这个 input1 和 input2 提供给我在不同文件(假设 test_this.py)中的函数,用于两个不同的函数.以下是基于以上信息的问题...

I feed this input1 and input2 to my functions in a different file (let's say test_this.py) for two different functions. Here are the questions based on information above...

  1. 因为我不能只在 conftest.py 中声明局部变量,因为我不能简单地导入这个文件.有没有更好的方法在这里声明可以在 test_this.py 中的不同函数中使用的不同变量?在我对这些变量的实际测试中,我有五种不同的配置,在 conftest.py 中定义了许多不同的装置并将它们用作 test_this.py 中五个不同函数中的函数参数听起来很痛苦,我宁愿回到 unittest 类结构,定义我的变量并选择我想要的

  1. Since I can't just declare local variables in conftest.py as I can't simply import this file. Is there a better way of declaring different variables here that can be used in different functions in test_this.py ? I have five different configurations in my actual testing for these variables, defining that many different fixtures in conftest.py and use them as function argument in five different functions in test_this.py sounds painful, I would rather go back to unittest class structure, define my variables and pick and choose what I want

我是否应该只在 test_this.py 中声明全局变量并按照我想要的方式在函数中使用它们?似乎有点不是pythonic.此变量仅供此文件中的函数使用.

Should I just declare global variables in test_this.py and use them in the functions the way I want ? Seems a bit not pythonic. This variables are only used by the functions in this file.

假设我还有 test_that.py 和 test_them.py.如果我在这些不同的文件之间有一些共享变量,我将如何声明它们?只需在所有这些测试文件所在的目录中创建一个文件 calle variables.py 并在需要时进行导入?这样我就可以将所有数据单独保存.

Let's say I have test_that.py and test_them.py as well. If I have some shared variables between these different files, how would I declare them ? just create a file calle variables.py in the directory where all these test files are and do an import whenever I need ? This way I can keep all data in a separate.

我的印象是 pytest 不鼓励使用类来组织您的函数吗?我在网上读到的每个例子,似乎都只使用了固定装置的一堆函数.在pytest中定义类和方法并组织测试的配置是什么?

Is it my impression that pytest discourages using a class to organize your functions ? Every example I read online, it all seem to employ bunch of functions with fixtures only. What is a configuration of defining class and methods and organize tests in pytest ?

我有一个测试场景,我必须将一个函数的结果用于另一个函数.使用 pytest,我在函数末尾有一个断言,而不是返回,因此我无法将此函数用作夹具.我如何做到这一点?我知道这不是一个好的做法,因为我的一个测试依赖另一个测试,但有解决办法吗?

I have a test scenario where I have to use result of one function into another. With pytest, I have an assert that is at the end of a function not a return so I won't be able to use this function as a fixture. How do I accomplish this ? I know this is not a good practice that my one test relies on another but is there a work around ?

预先感谢您的回答.

推荐答案

1) 首先,您不仅可以在 conftest.py 中声明这些装置,还可以在您想要的每个 Python 模块中声明这些装置.您可以导入该模块.您也可以像使用 setUp 方法一样使用设备:

1) First of all, you can declare those fixtures not only in conftest.py, but in every Python module you want. And you can import that module. Also you can use fixtures in the same way as you used setUp method:

@pytest.fixture(scope='class')
def input(request):
    request.cls.varA = 1
    request.cls.varB = 2
    request.cls.varC = 3
    request.cls.modified_varA = 2

@pytest.usefixtures('input')
class TestClass:
    def test_1(self):
        do_something_with_self.varA, self.varB

    def test_2(self):
        do_something_with_self_modified_varA, self.varC

或者您可以在单独的装置中定义单独的变量:

or you can define separate variables in separate fixtures:

def fixture_a():
    return varA

def fixture_b():
    return varB

def fixture_c():
    return varC

def fixture_mod_A():
    return modified_varA

或者制作一个返回所有变量的夹具(为什么不呢?)或者甚至制作间接参数化的fixture,它可以根据您的选择返回变量(相当混乱的方式):

or make one fixture which returns all the variables (why not?) or even make indirect parametrized fixture which returns variables by your choice (quite confusing way):

@pytest.fixture()
def parametrized_input(request):
   vars = {'varA': 1, 'varB': 2, 'varC': 3}
   var_names = request.param
   return (vars[var_name] for var_name in var_names)

@pytest.mark.parametrize('parametrized_input', [('varA', 'varC')], indirect=True)
def test_1(parametrized_input)
   varA, varC = parametrized_input
   ...

或者,您甚至可以制作固定装置工厂,它可以即时为您制作固定装置.当您只有 5 个测试和 5 个变量配置时,这听起来很奇怪,但是当您同时获得数百个变量时,它就会很有用.

Or even you can make fixture factory which will make fixtures for you on the fly. Sounds curiously when you have only 5 tests and 5 configurations of variables, but when you get hundreds of both, it can be useful.

3) 当然可以.但是我建议你不要直接导入这个文件,而是使用命令行选项来指定要导入的文件.在这种情况下,您可以在不更改代码的情况下选择另一个带有变量的文件.

3) Of course you can. But I recommend you not to import this file directly, but use command line option pointing what file to import. In this case you can choose another file with variables without changing your code.

4) 我在测试中使用类,因为我是从鼻子测试迁移过来的.我没有提到在 pytest 中使用类的任何问题.

4) I use classes in my tests because I migrated from nosetest. I didn't mention any problem with using classes in pytest.

5) 在这种情况下,我建议您执行以下操作:拳头使功能具有所需的操作:

5) In that case I propose you to do the following: fist make the function with desired actions:

def some_actions(a, b):
    # some actions here
    ...
    return c

然后在测试和夹具中使用它:

then use it both in test and fixture:

def test():
    assert some_actions(1,2) == 10

@pytest.fixture()
def some_fixture():
     return some_actions(1,2)

这篇关于单元测试与 pytest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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