Unit-testing with dependencies between tests

前端 未结 6 2065
日久生厌
日久生厌 2020-12-14 16:49

How do you do unit testing when you have

  • some general unit tests
  • more sophisticated tests checking edge cases, depending on the general ones
6条回答
  •  情书的邮戳
    2020-12-14 17:02

    You may want use pytest-dependency. According to theirs documentation code looks elegant:

    import pytest
    
    @pytest.mark.dependency()
    @pytest.mark.xfail(reason="deliberate fail")
    def test_a():
        assert False
    
    @pytest.mark.dependency()
    def test_b():
        pass
    
    @pytest.mark.dependency(depends=["test_a"])
    def test_c():
        pass
    
    @pytest.mark.dependency(depends=["test_b"])
    def test_d():
        pass
    
    @pytest.mark.dependency(depends=["test_b", "test_c"])
    def test_e():
        pass
    

    Please note, it is plugin for pytest, not unittest which is part of python itself. So, you need 2 more dependencies (f.e. add into requirements.txt):

    pytest==5.1.1
    pytest-dependency==0.4.0
    

提交回复
热议问题