Skip to content

Commit

Permalink
Add docs on disabling strict key checking (#279)
Browse files Browse the repository at this point in the history
* Add docs on disabling strict key checking

[ci skip]

* UPdate docs
  • Loading branch information
michaelboulton authored and benhowes committed Feb 25, 2019
1 parent fd0875f commit f7c7e39
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
47 changes: 42 additions & 5 deletions docs/source/basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,7 @@ the same request in each test to re-fetch the data.

**NOTE**: At the time of writing, Tavern will by default not perform 'strict'
key checking on the top level keys in the response, but will perform it on all
keys below that. This 'legacy' behaviour will be changed in a future version, see
below for details.
keys below that. This 'legacy' behaviour will be changed in a future version, see below for details.

'Strict' key checking can be enabled or disabled globally, per test, or per
stage. 'Strict' key checking refers to whether extra keys in the response should
Expand Down Expand Up @@ -456,7 +455,17 @@ compatability.
The strictness setting does not only apply to the body however, it can also be
used on the headers and redirect query parameters.

This setting can be controlled in 3 different ways.
This setting can be controlled in 3 different ways, the order of priority being:

1. In the test/stage itself
2. Passed on the command line
3. Read from pytest config

This means that using the command line option will _not_ override any settings
for specific tests.

**NOTE**: 'strict' key checking can currently only be _enabled_ via the command line
and the pytest config file, not _disabled_.

### Command line

Expand All @@ -469,13 +478,21 @@ only want to check that it returns the data you want and not care about any
extra metadata sent with the response. This can be re-enabled per test or per
stage if wanted.

Example:

```shell
# Enable strict checking for body and headers only
py.test --tavern-strict body headers -- my_test_folder/
```

### In the Pytest config file

This behaves identically to the command line option, but will be read from
whichever configuration file Pytest is using.

```editorconfig
[pytest]
tavern-strict=body headers
```

### Per test

Strictness can also be enabled or disabled on a per-test basis. The `strict` key
Expand Down Expand Up @@ -508,6 +525,25 @@ stages:
id: 1
---

test_name: Just check for one thing in a big nested dict

# completely disable strict key checking for this whole test
strict: False

stages:
- name: Try to get user
request:
url: "{host}/users/joebloggs"
method: GET
response:
status_code: 200
body:
q:
x:
z:
a: 1
---

test_name: Make sure the headers and body match what I expect exactly

strict:
Expand Down Expand Up @@ -557,6 +593,7 @@ stages:
url: "{host}/users/joebloggs"
method: GET
response:
# Disable all strict key checking just for this stage
strict: False
status_code: 200
json:
Expand Down
7 changes: 5 additions & 2 deletions tavern/testutils/pytesthook/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ def load_global_cfg(pytest_config):

global_cfg["variables"] = format_keys(loaded_variables, tavern_box)

strict = []

if pytest_config.getini("tavern-strict") is not None:
# Lowest priority
strict = pytest_config.getini("tavern-strict")
if isinstance(strict, list):
if any(
Expand All @@ -91,10 +94,10 @@ def load_global_cfg(pytest_config):
"Invalid values for 'strict' use in config file"
)
elif pytest_config.getoption("tavern_strict") is not None:
# Middle priority
strict = pytest_config.getoption("tavern_strict")
else:
strict = []

# Can be overridden in tests
global_cfg["strict"] = strict

global_cfg["backends"] = {}
Expand Down

0 comments on commit f7c7e39

Please sign in to comment.