From 7c57e650da7c1342301df6c31f0c958a2aea09ef Mon Sep 17 00:00:00 2001 From: Aby James <74111044+8bitaby@users.noreply.github.com> Date: Tue, 19 Mar 2024 00:00:17 +0530 Subject: [PATCH] Update django.py cheat sheet to include unit tests and coverage Extend Django.py Cheat Sheet: Incorporate Unit Testing and Coverage Information This commit enhances the existing Django.py cheat sheet by integrating guidance on unit testing and coverage analysis. The update includes detailed instructions on writing unit tests within Django applications, setting up test fixtures, executing tests locally, and leveraging coverage tools for assessing code coverage. Changes: - Added comprehensive instructions on writing unit tests for Django applications. - Included guidance on setting up test fixtures and organizing test files. - Provided commands and examples for running tests locally using Django's testing framework. - Introduced information on using test coverage tools like `coverage.py` to assess code coverage. - Enhanced the documentation to ensure clarity and completeness. This update aims to empower developers with the necessary knowledge and resources to ensure the robustness and reliability of their Django projects through effective testing and coverage analysis. --- backend/django.py | 74 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/backend/django.py b/backend/django.py index 50f53bd8..944567c1 100644 --- a/backend/django.py +++ b/backend/django.py @@ -118,3 +118,77 @@ # 8. $ django-admin startproject myproject # 9. $ django-admin startapp myapp # 10. $ python manage.py runserver + +# ***************************************************************************** +# Writing unit tests in django projects +# ***************************************************************************** + +# 1. Set Up Your Test Environment: +# Before writing tests, ensure that you have your Django project set up. Typically, you'll have a structure similar to this: +myproject/ +├── myproject/ +│ ├── __init__.py +│ ├── settings.py +│ ├── urls.py +│ └── wsgi.py +├── myapp/ +│ ├── __init__.py +│ ├── models.py +│ ├── views.py +│ └── tests.py # This is where your tests will go +├── manage.py +└── venv/ +# 2. Create test cases by subclassing django.test.TestCase or django.test.TransactionTestCase +# myapp/tests.py +``` +from django.test import TestCase +from .models import YourModel + +class YourModelTestCase(TestCase): + @classmethod + def setUpClass(cls): + # Set up resources for the entire test case class + pass + + @classmethod + def tearDownClass(cls): + # Clean up resources for the entire test case class + pass + + def setUp(self): + # Set up resources before each test method + pass + + def tearDown(self): + # Clean up resources after each test method + pass +``` +# --> setUp(): This method is called before each test method is executed. You can use it to set up any necessary state or resources needed for the test. +# --> tearDown(): This method is called after each test method is executed. It's used to clean up any resources or state that was set up in setUp(). +# 3. Configure your test database: Django creates a separate database for running tests, ensuring that your production data is not affected. This is specified in your settings.py file. +# settings.py +``` +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + }, + 'test': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'test_db.sqlite3', + } +} +``` +# 4. Run your tests: Django provides a management command, manage.py test, to run your tests. Navigate to your project directory in the terminal and run: +$ python manage.py test +$ python manage.py test your_app_name +$ python manage.py test your_app_name.YourModelTestCase +$ python manage.py test your_app_name.YourModelTestCase.test_something +# Django will discover your tests and run them. It will output the results to the terminal. +# 5. View test coverage: You may also want to measure the coverage of your tests. To do this, you can use third-party packages like coverage. First, install it: +$ pip install coverage +$ coverage run manage.py test +# 6. After running tests, you can generate a report: +$ coverage report +# This will show you which parts of your code are covered by tests and which are not. +