Skip to main content

Test tool for overridable json fixtures

Project description

[![Build Status](https://travis-ci.org/internap/jabstract.svg?branch=master)](https://travis-ci.org/internap/jabstract)

# Welcome unit testers!

## That's not cool:

```
def test_something():
api_response = {
"field1": "value1",
"field2": "value2",
....
"field37": "value37",
....
"field7632": "value7632"
}

myapi.return_value = api_response

result = production_code()

assert result == "value37"
```

^ This is annoying if your `production_code` method only uses field37 right?

Of course you could only define the field37 in your test but if a log or something else irrelevant to THIS test uses another field, it has to be there even if it's irrelevant to THIS test.

## That's prettier:


```
api_response = jabstract({
"field1": "value1",
"field2": "value2",
....
"field37": "value37",
....
"field7632": "value7632"
})

def test_something():
myapi.return_value = api_response(field37="value37")

result = production_code()

assert result == "value37"
```

^ The test is so much more beautiful!

# USAGE

Declare your json responses somewhere:

```
from jabstract import jabstract

api_response = jabstract({
... json-ish payload ...
})
```

Then use it in your tests by defining only relevant fields:

```
.return_value = api_response(
key=value
)
```

It even supports complex payloads!

```
api_response = jabstract({
"client": {
"name": "John doe",
"email": "johndoe@example.org"
}
})

.return_value = api_response(
client=dict(name="Foobar")
)
```

\* note that `response["client"]["email"]` will keep its default value.

# Best practices

Tests using jabstracted payload should define only what is relevant to the test, not less, not more, so that it is obvious to the human eye where a value come from

let's test this code
```
def name_getter(payload):
return payload["client"]["name"]
```

## **Good** example:
```
api_response = jabstract({
"client": {
"name": "John doe",
"email": "johndoe@example.org"
}
})

def test_name_getter():
payload = api_response(client=dict(name="Baboon 2.0"))

assert name_getter(payload) == "Baboon 2.0"
```

**Reviewer says** : Ohhh so it takes the name of the client from the payload... +2

## ~~BAD~~ example

```
api_response = jabstract({
"client": {
"name": "John doe",
"email": "johndoe@example.org"
}
})

def test_name_getter():
payload = api_response()

assert name_getter(payload) == "John doe"
```

**Reviewer says** : Who the hell is john doe... click on payload... ahhh i see... meh, +1

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jabstract-0.1.3.tar.gz (8.1 kB view details)

Uploaded Source

File details

Details for the file jabstract-0.1.3.tar.gz.

File metadata

  • Download URL: jabstract-0.1.3.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.5.6

File hashes

Hashes for jabstract-0.1.3.tar.gz
Algorithm Hash digest
SHA256 0ce86fffc005d6d11b878d2e779fae310b839d556958c71d2e0e0453e78a2da5
MD5 b3ca91b1e2a18870dcdc0c4531f53622
BLAKE2b-256 039fd1c4df3c2951dc9f5681d02515b3a42db4588740c51e54dddd49d1d2908d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page