什么时候PyTest测试应该成为Class的一部分,什么时候它们应该是独立的方法?

时间:2018-05-22 01:19:26

标签: python unit-testing pytest

我已经进行了一些测试,这些测试使用固定装置进行一些设置和拆卸,我不确定构建它的最佳方法。

doco中的示例显示了独立测试和类测试分组

这两个例子在非常基本的层面上展示了选项,并返回相同的结果。

  1. 独立

    import pytest
    
    
    @pytest.fixture
    def other():
        # setup
        yield
        # teardown
    
    def test_one():
        assert 1 == 1
    
    def test_two():
        assert 0 != 1
    
  2. 分组

    import pytest
    
    
    class TestClass:
    
        @pytest.fixture
        def other(self):
            # setup
            yield  
            # teardown
    
        def test_one(self):
            assert 1 == 1
    
        def test_two(self):
            assert 0 != 1
    
  3. 我想了解何时应该使用每个选项,以及每个选项的好处是什么?是否有严格的规则,或者是品味问题,等等。

    由于 西蒙。

0 个答案:

没有答案
相关问题