如何在 Python 中编写像 Jasmine 这样的嵌套测试用例

S.H*_*iba 2 python unit-testing

Jasmine 可以编写这样的嵌套测试用例:

  describe('func1', () => {
    it('case1', () => {

    })
    it('case2', () => {

    })
  })
  describe('func2', () => {
    it('case1', () => {

    })
    it('case2', () => {

    })
  })
Run Code Online (Sandbox Code Playgroud)

我想用Python写同样的东西。
我在WWW上找不到它。

Vis*_*ioN 7

从Python 3.4开始,您可以在unittest测试用例类中创建子测试(当然,如果您使用标准unittest库进行测试):

class MyTestCase(unittest.TestCase):
    def test_func1(self):
        with self.subTest('case1'):
            ...

        with self.subTest('case2'):
            ...

    def test_func2(self):
        with self.subTest('case1'):
            ...

        with self.subTest('case2'):
            ...
Run Code Online (Sandbox Code Playgroud)

更多: https://docs.python.org/3/library/unittest.html#distinguishing-test-iterations-using-subtests