How do I run multiple Python test cases in a loop?

前端 未结 4 1609
忘了有多久
忘了有多久 2021-02-03 21:50

I am new to Python and trying to do something I do often in Ruby. Namely, iterating over a set of indices, using them as argument to function and comparing its results with an a

4条回答
  •  面向向阳花
    2021-02-03 22:30

    In python world two most popular options to write tests are:

    • Unittest
    • pytest

    In pytest you parametrize tests very easly:

    @pytest.mark.parametrize(('param1', 'param2'),[
                             (1, 'go'),
                             (2, 'do not go')])
    def test_me(param1, param2):
        # write test
    

    This will produce nice output also while running tests:

    go.py:2: test_me[1-go] PASSED
    go.py:2: test_me[2-do not go] PASSED
    

    I am using pytest for two years now and it's very nice tool. You have many features there. Besides parametrization there are fixtures also, very very nice assertions (you do not need to write assertEqual, just assert a==b and pytest can generate very nice and helpful output for it.)

提交回复
热议问题