Skip to main content
[Hypothesis](https://github.com/HypothesisWorks/hypothesis-python) strategy for
[Django REST framework](http://www.django-rest-framework.org/) serializers.


Generate data that is valid according to a DRF serializer

### Known issues

* Only tested with python3.6
* `URLField` can be slow when generating the first example
* `*RelatedField` gets first object in queryset, not random
* Not all fields are supported, known non-working:
- FileField
- FilePathField
- ImageField
- ModelField


### Example

```bash
pip install hypothesis-drf
```

```python
from hypothesis_drf import from_serializer, from_field
from rest_framework import serializers


class ExampleSerializer(serializers.Serializer):
name = serializers.CharField(min_length=3, max_length=8)
amount = serializers.IntegerField(min_value=200, max_value=500)


from_serializer(ExampleSerializer).example()
# {'amount': 391, 'name': '\U00053a6b&\U00030fee$.'}

from_field(serializers.FloatField(min_value=-10, max_value=22)).example()
# -8.499125311228873

```


For HyperLinkedRelatedField (and variants) you must pass an instantiated serializer as
they require access to the request via context:

```python
from hypothesis_drf import from_serializer

class RelatedExample(serializers.ModelSerializer):
class Meta:
model = SourceModel
fields = ('target', )

target = HyperLinkedRelatedField(queryset=TargetModel.objects.all())

from_serializer(RelatedExample(context={'request': request})).example()
# {'target': 'http://example.com/something/'}
```


### Custom fields

Provide `hypothesis_strategy` on the field:

```python
from rest_framework import fields
from hypothesis import strategies as st

class MyField(fields.Field):
hypothesis_strategy = st.booleans()

# ...
```

Release files for hypothesis-drf 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hypothesis-drf 1.0.0
File Size Uploaded
hypothesis-drf-1.0.0.tar.gz 8.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for hypothesis-drf 1.0.0
File Interpreter ABI Platform
hypothesis_drf-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 15.1 kB

Release files / hypothesis-drf-1.0.0.tar.gz

Download URL hypothesis-drf-1.0.0.tar.gz
Size 8.5 kB
Tags Source
SHA-256 checksum
How to use checksums
f05d4c1b93627c237585b7988de7f02b900aa02494d197adb025dc3e77faa90e
BLAKE2b-256 checksum
How to use checksums
69933a61da055b8a2f7fb5c5ca0d3bbf701713360ede472071a1e891de45d15d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / hypothesis_drf-1.0.0-py3-none-any.whl

Download URL hypothesis_drf-1.0.0-py3-none-any.whl
Size 6.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cff9a2d3878aa4a4414a852e7f0006c4839e3fabae45b897fb07cec361dfd2a4
BLAKE2b-256 checksum
How to use checksums
b9bdcbdbcf1d59c690540ebeae69e82207817b6a6ca119b230d59e1f18bdf4ec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.0.7

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page