API¶
Unittest/django support¶
ApiCheckerMixin¶
-
class
drf_api_checker.unittest.
ApiCheckerMixin
[source]¶ Mixin to enable API contract check
How to use it:
- implement get_fixtures() to create data for test. It should returns a dictionary
- use self.assert<METHOD>(url) to check urls contract
Example:
- class TestAPIAgreements(ApiCheckerMixin, TestCase):
- def get_fixtures(self):
- return {‘customer’: CustomerFactory()}
- def test_customer_detail(self):
- url = reverse(“customer-detail”, args=[self.get_fixture(‘customer’).pk]) self.assertGET(url)
ApiCheckerBase¶
-
class
drf_api_checker.unittest.
ApiCheckerBase
[source]¶ Custom _type_, intended to be used as metaclass. It will create a test for each url defined in URLS in the format
test__<normalized_url_path>
, if a method with the same name is found the creation is skipped reading this as an intention to have a custom test for that url.- class TestAPIIntervention(TestCase, metaclass=ApiCheckerBase):
- URLS = [
- reverse(“intervention-list”), reverse(“intervention-detail”, args=[101]),
]
- def get_fixtures(cls):
- return {‘intervention’: InterventionFactory(id=101),
- ‘result’: ResultFactory(), }
running this code will produce…
… test_url__api_v2_interventions (etools.applications.partners.tests.test_api.TestAPIIntervention) … ok test_url__api_v2_interventions_101 (etools.applications.partners.tests.test_api.TestAPIIntervention) … ok …