diff --git a/rest_framework-stubs/test.pyi b/rest_framework-stubs/test.pyi index 00f5a6617..0b862369e 100644 --- a/rest_framework-stubs/test.pyi +++ b/rest_framework-stubs/test.pyi @@ -81,14 +81,18 @@ class APIClient(APIRequestFactory, DjangoClient): class APITransactionTestCase(testcases.TransactionTestCase): client_class: Type[APIClient] = ... + client: APIClient class APITestCase(testcases.TestCase): client_class: Type[APIClient] = ... + client: APIClient class APISimpleTestCase(testcases.SimpleTestCase): client_class: Type[APIClient] = ... + client: APIClient class APILiveServerTestCase(testcases.LiveServerTestCase): client_class: Type[APIClient] = ... + client: APIClient class URLPatternsTestCase(testcases.SimpleTestCase): ... diff --git a/tests/typecheck/test_test.yml b/tests/typecheck/test_test.yml new file mode 100644 index 000000000..69940d325 --- /dev/null +++ b/tests/typecheck/test_test.yml @@ -0,0 +1,28 @@ +- case: test_testcases_client_api + parametrized: + - test_class: APITransactionTestCase + - test_class: APITestCase + - test_class: APISimpleTestCase + - test_class: APILiveServerTestCase + main: | + from rest_framework import test, status + + class MyTest(test.{{ test_class }}): + def test_example(self) -> None: + reveal_type(self.client) # N: Revealed type is "rest_framework.test.APIClient" + response = self.client.get('/', format="json") + self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) + self.assertEqual(response.data, {"detail": {"not_found"}}) + +- case: test_testcases_client_api_urlpatterns + main: | + from typing import Union, List + from django.urls import URLPattern, URLResolver + from rest_framework import test, status + + class MyTest(test.URLPatternsTestCase): + urlpatterns : List[Union[URLPattern, URLResolver]] = [] + def test_example(self) -> None: + reveal_type(self.client) # N: Revealed type is "django.test.client.Client" + response = self.client.get('/', format="json") + self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)