我使用硒端到端测试,我不知道如何使用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?


当前回答

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 

其他回答

我不确定我在你最初的问题中得到了使用Selenium的细节,但如果你只是问如何使用更经典的setUp/tearDown风格,Pytest支持大多数单元测试功能,所以你可以这样做:

import unittest


class TestHello(unittest.TestCase):

    def setUp(self):
        print('running setUp')

    def test_one(self):
        print('running test_one')

    def test_two(self):
        print('running test_two')

    def tearDown(self):
        print('running tearDown')

生产:

$ pytest -s -v
====================== test session starts =======================
platform linux -- Python 3.8.2, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- /gnu/store/nckjv3ccwdi6096j478gvns43ssbls2p-python-wrapper-3.8.2/bin/python
cachedir: .pytest_cache
hypothesis profile 'default' -> database=DirectoryBasedExampleDatabase('/tmp/test/.hypothesis/examples')
rootdir: /tmp/test
plugins: hypothesis-5.4.1
collected 2 items                                                

test_hw.py::TestHello::test_one running setUp
running test_one
running tearDown
PASSED
test_hw.py::TestHello::test_two running setUp
running test_two
running tearDown
PASSED

这可能会有帮助http://docs.pytest.org/en/latest/xunit_setup.html

在我的测试套件中,我将测试用例分组为类。对于该类中所有测试用例所需的设置和拆卸,我使用setup_class(cls)和teardown_class(cls)类方法。

对于每个测试用例所需的设置和拆除,我使用setup_method(方法)和teardown_method(方法)

例子:

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

现在当我运行我的测试时,当TestClass执行开始时,它会记录详细信息,当它开始执行时,当它结束执行时,方法也是如此。

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

希望能有所帮助!

如果您添加了@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/

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

因为你的例子使用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,因为它们是一个更强大的概念。

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