Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我如何正确地为我的pytest类设置和拆卸测试?_Python_Class_Object_Pytest - Fatal编程技术网

Python 我如何正确地为我的pytest类设置和拆卸测试?

Python 我如何正确地为我的pytest类设置和拆卸测试?,python,class,object,pytest,Python,Class,Object,Pytest,我使用selenium进行端到端测试,但我不知道如何使用setup\u class和teardown\u class方法 我需要在setup\u class方法中设置浏览器,然后执行一系列定义为类方法的测试,最后在teardown\u class方法中退出浏览器 但从逻辑上看,这似乎是一个糟糕的解决方案,因为事实上,我的测试将不使用类,而是使用对象。我通过了每个测试方法中的selfparam,因此我可以访问对象的变量: class TestClass: def setup_clas

我使用selenium进行端到端测试,但我不知道如何使用
setup\u class
teardown\u class
方法

我需要在
setup\u class
方法中设置浏览器,然后执行一系列定义为类方法的测试,最后在
teardown\u class
方法中退出浏览器

但从逻辑上看,这似乎是一个糟糕的解决方案,因为事实上,我的测试将不使用类,而是使用对象。我通过了每个测试方法中的
self
param,因此我可以访问对象的变量:

class TestClass:
  
    def setup_class(cls):
        pass
        
    def test_buttons(self, data):
        # self.$attribute can be used, but not cls.$attribute?  
        pass
        
    def test_buttons2(self, data):
        # self.$attribute can be used, but not cls.$attribute?
        pass
        
    def teardown_class(cls):
        pass
    
甚至为类创建浏览器实例似乎也不正确。。它应该分别为每个对象创建,对吗

因此,我需要使用
\uuuu init\uuuuuuu
\uu del\uuuuu
方法,而不是
设置类
拆卸类

当您编写“定义为类方法的测试”时,您真的是指类方法(将其作为第一个参数的方法)还是常规方法(接收实例作为第一个参数的方法)

由于您的示例对测试方法使用了
self
,因此我假设使用后者,因此您只需使用
setup\u method

class Test:

    def setup_method(self, test_method):
        # configure self.attribute

    def teardown_method(self, test_method):
        # tear down self.attribute

    def test_buttons(self):
        # use self.attribute for test
测试方法实例被传递到
setup\u method
teardown\u method
,但如果您的setup/teardown代码不需要知道测试上下文,则可以忽略该实例。可以找到更多信息


我还建议您熟悉py.test,因为它们是一个更强大的概念。

正如@Bruno所建议的,使用pytest fixture是另一个解决方案,可以用于两个测试类,甚至只是简单的测试函数:

因此,运行
test\u 1…
会产生:

I setup... and now I'm testing things...
I setup ... but now I'm torn down...
请注意,夹具中引用了
stuff\u i\u setup
,允许该对象在与之交互的测试中被
setup
拆下。您可以想象,这对于持久对象(例如假设的数据库或某个连接)非常有用,在每次测试运行之前必须清除该对象以保持m隔离。

根据,当前设置和拆卸的最佳实践是使用
屈服
而不是
返回

import pytest

@pytest.fixture()
def resource():
    print("setup")
    yield "resource"
    print("teardown")

class TestResource:
    def test_that_depends_on_resource(self, resource):
        print("testing {}".format(resource))
运行它会导致

$ py.test --capture=no pytest_yield.py
=== test session starts ===
platform darwin -- Python 2.7.10, pytest-3.0.2, py-1.4.31, pluggy-0.3.1
collected 1 items

pytest_yield.py setup
testing resource
.teardown


=== 1 passed in 0.01 seconds ===
编写拆卸代码的另一种方法是,在fixture函数中接受一个,并使用执行一次或多次拆卸的函数调用其
请求.addfinalizer
方法:

import pytest

@pytest.fixture()
def resource(request):
    print("setup")

    def teardown():
        print("teardown")
    request.addfinalizer(teardown)
    
    return "resource"

class TestResource:
    def test_that_depends_on_resource(self, resource):
        print("testing {}".format(resource))

如果您添加了
@classmethod
装饰器,那么您的代码应该能够正常工作

@classmethod 
def setup_class(cls):
    "Runs once per class"

@classmethod 
def teardown_class(cls):
    "Runs at end of class"
请参见

这可能会有所帮助

在我的测试套件中,我将测试用例分组到类中。对于该类中所有测试用例所需的设置和拆卸,我使用
setup\u类(cls)
teardown\u类(cls)
classmethods

对于每个测试用例所需的设置和拆卸,我使用
setup\u方法(method)
teardown\u方法(methods)

例如:

lh = <got log handler from logger module>

class TestClass:
    @classmethod
    def setup_class(cls):
        lh.info("starting class: {} execution".format(cls.__name__))

    @classmethod
    def teardown_class(cls):
        lh.info("starting class: {} execution".format(cls.__name__))

    def setup_method(self, method):
        lh.info("starting execution of tc: {}".format(method.__name__))

    def teardown_method(self, method):
        lh.info("starting execution of tc: {}".format(method.__name__))

    def test_tc1(self):
        <tc_content>
        assert 

    def test_tc2(self):
        <tc_content>
        assert
lh=
类TestClass:
@类方法
def设置_类(cls):
info(“起始类:{}执行”.format(cls.\uuuuu name\uuuuuu))
@类方法
def拆卸类(cls):
info(“起始类:{}执行”.format(cls.\uuuuu name\uuuuuu))
def设置方法(自身、方法):
info(“开始执行tc:{}.”格式(方法名)
def拆卸方法(自身,方法):
info(“开始执行tc:{}.”格式(方法名)
def测试_tc1(自身):
断言
def测试_tc2(自身):
断言
现在,当我运行测试时,当TestClass执行开始时,它会记录开始执行、结束执行以及方法的详细信息

您可以在相应的位置添加其他安装和拆卸步骤

希望有帮助

import pytest
class Test:
    @pytest.fixture()
    def setUp(self):
        print("setup")
        yield "resource"
        print("teardown")

    def test_that_depends_on_resource(self, setUp):
        print("testing {}".format(setUp))
为了运行:

pytest nam_of_the_module.py -v 

所以你把它复制到你需要资源的每个测试文件中?但是这不是一个类设置,对吗?它会在类中的每个测试方法之前执行。在这种特殊情况下,它只在作为测试方法中的参数使用时才执行。例如,
test\u中的
resource
param,它依赖于\u资源(self,resource)
注意,您可以将fixture scope设置为“class”,autouse设置为true,以确保为每个类调用一次代码,而不必将其作为参数包含在任何测试调用中:``pytest.fixture(scope=“class”,autouse=true)def resource():print(“setup”)yield“resource”print(“teardown”)```链接更新:fixture比class方法弱:它们不允许销毁非它们创建的对象(这通常是真正必要的)。除此之外,感谢您提供的信息。当我将代码库从pytest的3.0.x版本升级到4.x版本时,我突然想到了这一点。一些较旧的代码使用了
setup\u类
模拟方法等,需要现代化。
setup\u类(self,foo,bar)
-->
setup\u方法(self,function,foo,bar)
这与文档中显示的内容几乎一模一样。我在文档中遇到的问题是,我很难理解上下文:self在传统上被称为self,而不是cls,因此这在我看来很奇怪,与类本身的上下文无关。Kiran(上面)提供了这个上下文。@cogniticlaeves“self传统上被称为self,而不是cls”是的,
self
用于实例方法,其中第一个参数是方法操作正在进行的特定对象实例,而
cls
用于绑定到类而不是类实例的
@classmethod
s(即对象)。嗨@Kiran,
setup\u类
setup\u方法
之间有什么区别?@imsrgadich,当您组织测试CA时
pytest nam_of_the_module.py -v