taverntesting/tavern

Curly braces in JSON keys of criteria causes error when test fails

iamsilk opened this issue · 0 comments

Curly braces in the contents of test criteria are meant for insertion of variables. However, you can use curly braces in keys without having it be substituted for variables. There aren't any issues when using this functionality when tests are passing.

When a test using curly braces in a JSON key fails, however, and error will occur if the test fails, causing pytest and Tavern to fail completely. Below is a log of this error occurring.

INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/usr/local/lib/python3.9/site-packages/box/box.py", line 488, in __getitem__
INTERNALERROR>     return super().__getitem__(item)
INTERNALERROR> KeyError: 'c5548c42-b4c8-41a4-8f4f-e4c1a33bf0b1'
INTERNALERROR> 
INTERNALERROR> The above exception was the direct cause of the following exception:
INTERNALERROR> 
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/usr/local/lib/python3.9/site-packages/tavern/util/dict_util.py", line 34, in _check_and_format_values
INTERNALERROR>     would_replace = formatter.get_field(field_name, [], box_vars)[0]
INTERNALERROR>   File "/usr/local/lib/python3.9/string.py", line 270, in get_field
INTERNALERROR>     obj = self.get_value(first, args, kwargs)
INTERNALERROR>   File "/usr/local/lib/python3.9/string.py", line 227, in get_value
INTERNALERROR>     return kwargs[key]
INTERNALERROR>   File "/usr/local/lib/python3.9/site-packages/box/box.py", line 509, in __getitem__
INTERNALERROR>     raise BoxKeyError(str(err)) from _exception_cause(err)
INTERNALERROR> box.exceptions.BoxKeyError: "'c5548c42-b4c8-41a4-8f4f-e4c1a33bf0b1'" 

Below are sample testing files that help explain the issue.

example.tavern.yaml:

test_name: Example test

stages:
  - name: Stage one
    request:
      url: "{host}/example"
      method: GET
    response:
      status_code: 200
      json: !include example_response.json

example_response.json:

{
    "{c5548c42-b4c8-41a4-8f4f-e4c1a33bf0b1}": "sample"
}