Pytest where to store expected data

后端 未结 4 990
有刺的猬
有刺的猬 2021-01-31 02:27

Testing function I need to pass parameters and see the output matches the expected output.

It is easy when function\'s response is just a small array or a one-line st

4条回答
  •  忘掉有多难
    2021-01-31 03:25

    I believe pytest-datafiles can be of great help. Unfortunately, it seems not to be maintained much anymore. For the time being, it's working nicely.

    Here's a simple example taken from the docs:

    import os
    import pytest
    
    @pytest.mark.datafiles('/opt/big_files/film1.mp4')
    def test_fast_forward(datafiles):
        path = str(datafiles)  # Convert from py.path object to path (str)
        assert len(os.listdir(path)) == 1
        assert os.path.isfile(os.path.join(path, 'film1.mp4'))
        #assert some_operation(os.path.join(path, 'film1.mp4')) == expected_result
    
        # Using py.path syntax
        assert len(datafiles.listdir()) == 1
        assert (datafiles / 'film1.mp4').check(file=1)
    

提交回复
热议问题