Skip to content

Instantly share code, notes, and snippets.

@Afeez1131
Last active January 21, 2024 07:06
Show Gist options
  • Save Afeez1131/2696dcf9a1512a1710d6bb008c866e88 to your computer and use it in GitHub Desktop.
Save Afeez1131/2696dcf9a1512a1710d6bb008c866e88 to your computer and use it in GitHub Desktop.
Basic outline to act as guide while writing test for model and forms, to be updated with views as well.

Form Tests:

  1. Empty Form:

    • Test an empty form to ensure it is invalid.
    • Verify that the form errors match the expected errors for required fields.
  2. Form with Invalid Data:

    • Test the form with intentionally invalid data.
    • Check that the form is marked as invalid and contains appropriate error messages.
  3. Form with Valid Data:

    • Test the form with valid data.
    • Ensure that the form is marked as valid.
  4. Form with Instance:

    • Test a form populated with an instance's data.
    • Verify that the form is initialized correctly with the instance data.
  5. Saving Form to Create Instance:

    • Test saving the form to create a new instance.
    • Confirm that the instance is created with the expected data.
  6. Form Widget Customization:

    • Test that widget attributes (e.g., CSS classes, placeholders) are applied correctly.
    • Check that widget customization does not interfere with form functionality.
  7. Form Widget Rendering:

    • Test that widgets are rendered as expected in the HTML form.
    • Verify that widget rendering aligns with your design expectations.
  8. Form Model Relationship:

    • If the form has a model relationship, test that it is correctly associated.
    • Check that the form works well with related models.
  9. Form Clean Method:

    • Test the clean method for the form.
    • Verify that custom validation in the clean method behaves as expected.
  10. Custom Field Testing:

    • If your form has custom fields, ensure they are validated correctly.
    • Test the behavior of custom widget attributes if applicable.

Model Tests:

  1. Instance Getting Created:

    • Test the creation of model instances.
    • Verify that instances are saved to the database as expected.
  2. Test __str__ Method:

    • Check that the __str__ method returns a meaningful string representation.
    • Test that the string representation is as expected.
  3. Test Automatic Fields (e.g., Created):

    • Verify that automatic fields (e.g., created_at, modified_at) are populated correctly.
    • Check that the automatic fields are updated appropriately during modifications.
  4. Test Unique Constraints:

    • Test the enforcement of unique constraints on fields.
    • Verify that trying to create instances with duplicate unique fields results in the expected behavior.
  5. Custom Fields Testing:

    • If your model has custom fields, ensure they are validated correctly.
    • Test any custom methods defined on the model.
  6. Testing Model Methods:

    • If you have custom methods in your model, test them for correctness.
    • Ensure that methods involving calculations or data processing behave as expected.
  7. Testing Model Manager:

    • Test the default manager's behavior.
    • If you have custom managers, ensure they work as intended.
  8. Database Integrity:

    • Ensure that the model enforces data integrity rules.
    • Test the behavior of constraints like unique, null, and default.
  9. Testing Signals (if applicable):

    • If you have signals connected to your model, test their behavior.
    • Verify that signals respond correctly to various model events.
  10. Data Migration Testing:

    • If you use Django migrations, test the migration process.
    • Ensure that existing data is migrated correctly when you make changes to your models.

View Tests:

  1. HTTP Status Codes:

    • Test that the view returns the correct HTTP status codes for different scenarios (e.g., 200 OK, 302 Redirect, 404 Not Found).
    • Check the status code when handling form submissions with invalid data.
  2. Template Rendering:

    • Test that the correct templates are being rendered.
    • Verify that the template includes the expected content.
    • Check the rendering of dynamic data within the templates.
  3. Context Data:

    • Ensure that the view passes the required data to the template context.
    • Test context data for different scenarios (e.g., authenticated user, user without permissions).
  4. URL Routing:

    • Verify that the URLs are correctly mapped to the respective views.
    • Test the behavior of URL parameters if applicable.
    • Check for proper handling of optional and required URL parameters.
  5. Authentication and Authorization:

    • Test views requiring authentication for proper redirection or login page display.
    • Check access control for views requiring specific permissions.
    • Test views with both authenticated and unauthenticated users.
  6. Form Handling:

    • Test the submission of forms associated with the view.
    • Check how the view handles invalid form submissions.
    • Test views with multiple forms if applicable.
  7. AJAX and API Views (if applicable):

    • Test API views for correct handling of requests and responses.
    • Verify proper handling of AJAX requests.
    • Check for the presence of appropriate headers in API responses.
  8. Security:

    • Ensure that the view guards against common security vulnerabilities (e.g., CSRF protection).
    • Test views with sensitive data to ensure proper access control.
  9. Session and Cookies:

    • Test views that interact with session data.
    • Check for the presence and content of cookies if applicable.
  10. Redirects:

    • Test views that perform redirects.
    • Verify that the redirect URL is constructed correctly.
  11. Query Parameters:

    • Test views that rely on query parameters.
    • Check how the view handles various combinations of query parameters.
  12. Caching:

    • Test views that use caching.
    • Verify that the caching behavior is as expected.
  13. Exception Handling:

    • Test views for proper exception handling.
    • Check how the view responds to different types of exceptions.
  14. Pagination (if applicable):

    • Test views that implement pagination.
    • Verify that the correct subset of data is displayed.
  15. Internationalization and Localization:

    • Test views with different languages and locales.
    • Ensure that translations are applied correctly.
  16. Third-Party Integrations (if applicable):

    • Test views that interact with third-party APIs or libraries.
    • Verify that error handling is robust for external dependencies.
  17. Performance:

    • Conduct performance testing on critical views.
    • Identify and optimize any performance bottlenecks.
  18. User Experience:

    • Check the user experience during different interactions with the view.
    • Verify that error messages provide meaningful feedback.
  19. Edge Cases:

    • Test views with edge cases and boundary conditions.
    • Verify that the view behaves correctly in exceptional scenarios.
  20. Testing URLs with Custom Query Parameters:

    • Test views that accept custom query parameters in the URL.
    • Verify that the view processes and responds to custom query parameters appropriately.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment