Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing Django Rest Framework Serializers with pytest

I could use pytest for testing models and views in my django project. Is it possible to use pytest for DRF serializers as well, appreciate pointers to samples.

like image 209
Kalpa Welivitigoda Avatar asked Mar 26 '17 16:03

Kalpa Welivitigoda


1 Answers

The following works:

def test_foo_serializer():
    from app.models import Model
    from app.serializers import ModelSerializer

    serializer = ModelSerializer()
    f = serializer.fields['field_name']
    obj = Model()

    assert f.to_representation(obj) == '0.00'
    obj.prop = 123
    assert f.to_representation(obj) == '1.23'
like image 167
blueyed Avatar answered Oct 14 '22 15:10

blueyed