IMBlues / IMBlues.github.io

My legacy blog, moved to https://github.com/IMBlues/nobelium
2 stars 0 forks source link

use-pytest-fixture-factory #5

Open utterances-bot opened 3 years ago

utterances-bot commented 3 years ago

善用 pytest fixture factory 构建结构优秀的单元测试

https://emergencyexit.xyz/use-pytest-fixture-factory.html

IMBlues commented 3 years ago

更新

可以使用 xfail 来代替有点傻的类型判断

class TestUtils:
    @pytest.mark.parametrize(
        "input, expected",
        [
            pytest.param("abc", "", marks=pytest.mark.xfail(raises=ValueError)),
        ],
    )
    def test_some_content(self, input, expected, make_fake_resp):
        """测试某些内容"""

        with mock.patch('some-need-patching-refrences') as _patch:
            _patch.side_effect = make_fake_resp(input)
            assert call_test(input) == expected

看起来虚浮多了 🥴