我使用硒端到端测试,我不知道如何使用setup_class和teardown_class方法。

我需要在setup_class方法中设置浏览器,然后执行一堆定义为类方法的测试,最后在teardown_class方法中退出浏览器。

但从逻辑上讲,这似乎是一个糟糕的解决方案,因为实际上我的测试不会对类有效,而是对对象有效。我在每个测试方法中传递self param,所以我可以访问对象的vars:

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
    

甚至为类创建浏览器实例似乎也不正确。它应该分别为每个对象创建,对吧?

所以,我需要使用__init__和__del__方法,而不是setup_class和teardown_class?


当前回答

正如@Bruno所建议的,使用pytest fixture是另一种解决方案,它可以用于两个测试类,甚至只是简单的测试函数。下面是一个测试python2.7函数的例子:

import pytest

@pytest.fixture(scope='function')
def some_resource(request):
    stuff_i_setup = ["I setup"]

    def some_teardown():
        stuff_i_setup[0] += " ... but now I'm torn down..."
        print stuff_i_setup[0]
    request.addfinalizer(some_teardown)

    return stuff_i_setup[0]

def test_1_that_needs_resource(some_resource):
    print some_resource + "... and now I'm testing things..."

运行test_1…生产:

I setup... and now I'm testing things...
I setup ... but now I'm torn down...

注意,stuff_i_setup在fixture中被引用,允许为与之交互的测试设置和拆除该对象。您可以想象,这对于持久对象(例如假设的数据库或某些连接)非常有用,这些对象必须在每次测试运行之前清除,以保持它们之间的隔离。

其他回答

如果您添加了@classmethod装饰器,那么您的代码应该能像您期望的那样工作。

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

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

参见http://pythontesting.net/framework/pytest/pytest-xunit-style-fixtures/

根据Fixture终结/执行拆卸代码,当前安装和拆卸的最佳实践是使用yield而不是return:

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))
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 

正如@Bruno所建议的,使用pytest fixture是另一种解决方案,它可以用于两个测试类,甚至只是简单的测试函数。下面是一个测试python2.7函数的例子:

import pytest

@pytest.fixture(scope='function')
def some_resource(request):
    stuff_i_setup = ["I setup"]

    def some_teardown():
        stuff_i_setup[0] += " ... but now I'm torn down..."
        print stuff_i_setup[0]
    request.addfinalizer(some_teardown)

    return stuff_i_setup[0]

def test_1_that_needs_resource(some_resource):
    print some_resource + "... and now I'm testing things..."

运行test_1…生产:

I setup... and now I'm testing things...
I setup ... but now I'm torn down...

注意,stuff_i_setup在fixture中被引用,允许为与之交互的测试设置和拆除该对象。您可以想象,这对于持久对象(例如假设的数据库或某些连接)非常有用,这些对象必须在每次测试运行之前清除,以保持它们之间的隔离。

当你写“定义为类方法的测试”时,你真的指的是类方法(将类作为第一个参数接收的方法)还是普通方法(将实例作为第一个参数接收的方法)?

因为你的例子使用self作为测试方法,我假设是后者,所以你只需要使用setup_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_method和teardown_method,但是如果你的setup/teardown代码不需要知道测试上下文,可以忽略它。更多信息可以在这里找到。

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