How do I unit test django urls?

后端 未结 2 1150
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-30 08:46

I have achieved 100% test coverage in my application everywhere except my urls.py. Do you have any recommendations for how I could write meaningful unit te

2条回答
  •  执念已碎
    2021-01-30 09:18

    Just complementing the answer from @karthikr (on the basis of his)

    -> you may assert that the view in charge of resolve is the one you'd expect using resolver.func.cls

    example

    from unittest import TestCase
    from django.urls import resolve
    
    from foo.api.v1.views import FooView
    
    
    class TestUrls(TestCase):
        def test_resolution_for_foo(self):
            resolver = resolve('/api/v1/foo')
            self.assertEqual(resolver.func.cls, FooView)
    
    

提交回复
热议问题