Giter Site home page Giter Site logo

virtualstock / incuna-test-utils Goto Github PK

View Code? Open in Web Editor NEW

This project forked from incuna/incuna-test-utils

0.0 0.0 0.0 197 KB

TestCases and other helpers for testing Django apps.

License: BSD 2-Clause "Simplified" License

Python 99.46% Makefile 0.48% HTML 0.07%

incuna-test-utils's Introduction

incuna-test-utils

Build Status

Incuna Test Utils is a collection of TestCases and other helpers for testing Django apps.

TestCases

These are found in incuna_test_utils.testcases.

urls.URLTestCase

URLTestCase adds assert_url_matches_view to check a url has been configured to use the correct view.

request.BaseRequestTestCase

BaseRequestTestCase provides various helper methods for working with django views:

  • get_view returns a view callable based on a view attribute set on the TestCase class. view can be either a function-based or a class-based view.
  • view_instance returns an instance of a class-based view attribute set on the TestCase class. view_instance accepts a request and *args and **kwargs. These are set on the view instance.
  • add_session_to_request gives a request a session.
  • create_user returns a user using either AnonymousUser or a user_factory attribute set on the TestCase. The user_factory should have a create method that returns a user. factory_boy is recommended.
  • create_request wraps Django's RequestFactory to provide useful defaults. It returns a request with user and _messages attributes. It can also set DATA and session on the request.

integration.BaseAdminIntegrationTestCase

BaseAdminIntegrationTestCase provides a TestCase to test the django admin actions such as add, change, changelist and delete. BaseAdminIntegrationTestCase should be subclassed and should define two attributes:

  • a user_factory to create an authenticated client;
  • a model to test.

Example:

from incuna_test_utils.testcases.integration import BaseAdminIntegrationTestCase


class TestUserAdmin(BaseAdminIntegrationTestCase):
    user_factory = factories.UserFactory
    model = ModelToTest

    def test_admin_add_page(self):
        response = self.get_admin_add_page()
        self.assertEqual(response.status_code, 200)

    ...

integration.BaseIntegrationTestCase

BaseIntegrationTestCase extends BaseRequestTestCase and adds more helper methods useful for integration tests:

  • access_view creates a request, calls the TestCase's view and returns a response.
  • render_to_str renders a response using a request, response.template_name and response.context_data. If a request is not provided, render_to_str uses response.request.
  • access_view_and_render_response wraps access_view and render_to_str. It also checks the response.status_code is as expected. The default expected_status is 200 (HTTP_OK).
  • assert_presence checks that an item does or doesn't appear in a container.
  • assert_count checks that an item appears in a container an expected number of times.
  • assert_presence_multiple and assert_count_multiple run one or more assertions in a single method call.
  • render_view_and_assert_content and render_view_and_assert_content_counts combine a call to access_view_and_render_response with a multiple-assert call on the result.

api_request.BaseAPIRequestTestCase

BaseAPIRequestTestCase extends BaseRequestTestCase for use with django-rest-framework.

  • create_request is overriden to use rest framework's APIRequestFactory. It also sets request.format to 'json'. If called with auth=True (the default), create_request also calls force_authenticate.

Factories

These are found in incuna_test_utils.factories. They require factory_boy.

user.BaseUserFactory

This defines a simple factory with an email attribute. This can be used with a custom User model that has these fields:

    class UserFactory(BaseUserFactory):
        class Meta:
            model = User

user.BaseAdminUserFactory

BaseAdminUserFactory defines a user with is_active, is_staff and is_superuser to True. This factory also defines a post hook which sets a default password accessible with raw_password.

feincms_page.PageFactory

This factory can be used to create instances of Feincms's Page model.

compat

compat provides a few miscelleaneous helpers useful for testing cross-version code:

  • DJANGO_LT_19 is True if django.VERSION is less than 1.9.
  • Python2AssertMixin aliases python 2.7 assert methods to match the python 3 api.
    • TestCase.assertItemsEqual is aliased as assertCountEqual
    • TestCase.assertRegexpMatches is aliased as assertRegex

incuna-test-utils's People

Contributors

lilyfoote avatar adam-thomas avatar meshy avatar maxpeterson avatar kevinetienne avatar mattack108 avatar minglee01 avatar rdembitskyi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.