Pytest where to store expected data

后端 未结 4 903
有刺的猬
有刺的猬 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:14

    If you only have a few tests, then why not include the data as a string literal:

    expected_data = """
    Your data here...
    """
    

    If you have a handful, or the expected data is really long, I think your use of fixtures makes sense.

    However, if you have many, then perhaps a different solution would be better. In fact, for one project I have over one hundred input and expected-output files. So I built my own testing framework (more or less). I used Nose, but PyTest would work as well. I created a test generator which walked the directory of test files. For each input file, a test was yielded which compared the actual output with the expected output (PyTest calls it parametrizing). Then I documented my framework so others could use it. To review and/or edit the tests, you only edit the input and/or expected output files and never need to look at the python test file. To enable different input files to to have different options defined, I also crated a YAML config file for each directory (JSON would work as well to keep the dependencies down). The YAML data consists of a dictionary where each key is the name of the input file and the value is a dictionary of keywords that will get passed to the function being tested along with the input file. If you're interested, here is the source code and documentation. I recently played with the idea of defining the options as Unittests here (requires only the built-in unittest lib) but I'm not sure if I like it.

提交回复
热议问题