Pytest where to store expected data

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

    Think if the whole contents of the config file really needs to be tested.

    If only several values or substrings must be checked, prepare an expected template for that config. The tested places will be marked as "variables" with some special syntax. Then prepare a separate expected list of the values for the variables in the template. This expected list can be stored as a separate file or directly in the source code.

    Example for the template:

    ALLOWED_HOSTS = ['{host}']
    DEBUG = {debug}
    DEFAULT_FROM_EMAIL = '{email}'
    

    Here, the template variables are placed inside curly braces.

    The expected values can look like:

    host = www.example.com
    debug = False
    email = webmaster@example.com
    

    or even as a simple comma-separated list:

    www.example.com, False, webmaster@example.com
    

    Then your testing code can produce the expected file from the template by replacing the variables with the expected values. And the expected file is compared with the actual one.

    Maintaining the template and expected values separately has and advantage that you can have many testing data sets using the same template.

    Testing only variables

    An even better approach is that the config generation method produces only needed values for the config file. These values can be easily inserted into the template by another method. But the advantage is that the testing code can directly compare all config variables separately and in clear way.

    Templates

    While it is easy to replace the variables with needed values in the template, there are ready template libraries, which allow to do it only in one line. Here are just a few examples: Django, Jinja, Mako

提交回复
热议问题