# Python : Django : Testing ## Classes ```python unittest.TestCase ^^^ test.testcases.SimpleTestCase <<< adds self.client; if using database, upgrade to TTC|TC|LSTC ^^^ TransactionTestCase <<< supports fixtures; resets DB via truncation; use this when testing tx behavior ^^^ ^^^ TestCase LiveServerTestCase <<< launches live HTTP server in separate thread for Selenium ^^^ resets DB via Tx rollback ``` ## API ```python from django.test import Client, TestCase response = self.client.get( "/foo" ) # built-in to TestCase # override defaults c = Client( json_encoder=DjangoJSONEncoder headers=None ) response = c.get( "/path", data=None|dict, headers=None|dict ) response = c.post( "/path", data=None|dict, content_type=MULTIPART_CONTENT, headers=None|dict ) if content_type="application/json", data is serialized using json.dumps() response.status_code int response.content bytestring response.json() content parsed ``` ## TestCases ```python class MyTestCase( TestCase ): client_class = MyCustomClient fixtures = [ "foo", "bar.json" ] def test__foo( self ): ... ``` ## Fixtures Fixture paths: `APP/fixtures/FIXTURE.json|xml` ```json [{ "model": "myapp.log", "pk": "000323fc-ec1a-4486-85bf-34a0811e0e9b", "fields": { "created": "2024-05-30T05:15:22.111Z", "modified": "2024-05-30T05:15:22.111Z", "name": "2024-03-19_17-40-58_thingy9", "file_size": 1838050, "upload_ts": "2024-03-19T18:18:48Z", "drone_id": "thingy9", "build_version": "v1.7.1-prod-ed71446c139672dd4c0031f73d936e0fafd9209f+", "start_ts": "2024-03-19T17:40:58Z", "end_ts": "2024-03-19T18:17:55Z", "takeoff_ts": "2024-03-19T18:04:36Z", "row1_tz": "America/New_York", "dest_address": "225 State St, Hackensack, NJ 07601, USA", "battery_cname": "", "user_email": "" } }] ```