from django.test import Client def test_my_view(self): client = Client() response = client.get('/my-url/') self.assertEqual(response.status_code, 200)
from django.test import Client def test_login(self): client = Client() response = client.post('/login/', {'username': 'testuser', 'password': 'testpass'}) self.assertRedirects(response, '/dashboard/') def test_logout(self): client = Client() client.login(username='testuser', password='testpass') response = client.get('/logout/') self.assertRedirects(response, '/')In this example, we use the Django.test Client to simulate a POST request to a login view with a username and password. We then use an assertion to check that the response redirects to the dashboard page. We also use the Client to test a logout view. We first use the `login()` method to authenticate the user, and then simulate a GET request to the logout view. We then use an assertion to check that the response redirects to the home page. Overall, the Django.test Client is a powerful tool in Django for testing web applications. By simulating requests and responses, developers can thoroughly test the functionality and behavior of their applications without having to run them on a server.