Moped Documentation
  • Welcome 👋
  • User Guides
    • Getting started
    • Map a project
  • Product Management
    • User communication
    • User management
    • User analytics
    • Local testing
    • Release process
    • Patch release process
    • MUI X Pro License
    • Integrations
      • Dataset documentation
      • ArcGIS Online
      • eCapris
      • Power BI
    • Features
  • Dev Guides
    • DB Docs & Data dictionary
    • Database backup policy
    • Moped Read Replica
    • How-to's
      • How do I start the Hasura cluster locally?
      • How do I launch the Hasura Console?
      • How do I get a JWT token?
      • How to ping the GraphQL API
      • How to ping the REST API
      • How do I connect a database with Postgres GUIs?
      • How do I connect to the RDS instance?
      • How to load production data into a local instance
      • How do I update seed data?
    • Hasura
      • Hasura Roles
      • Hasura Migrations
        • Getting Started
        • Installing the Hasura CLI
        • Configuration Files
        • Hasura Migration Principles
        • The Migration file format
        • Development
        • Hasura Seed Data
        • Running the Hasura Cluster Locally (video)
        • Create a migration: Exercise 1 (video)
        • Create a migration: Exercise 2 (video)
        • Latest hasura-cluster features
    • User Management
    • Authentication
      • Authentication Architecture
      • DynamoDB & Cognito
      • Secrets Manager & Cognito
      • Hasura & Cognito
      • React & Cognito
      • Flask API & Cognito
      • Single Sign-On with CTM
    • Code organization
    • API
      • Configuration Files
      • Testing
      • User Management API
    • Maps and geospatial data
      • Access tokens and API keys
      • Map libraries
      • Map data
      • Map styles
      • Map layers and basemaps
      • React patterns
      • V1 Archive
        • Map libraries
        • Map data
        • Map custom hooks
        • Map styles
        • Map layers and basemaps
    • UI access control
    • Design system
      • Branding
      • Component styles
      • Text content
    • Activity Log
      • Architecture
      • GitHub Actions and Deployment of Updates
      • Hasura Event Logs and Truncate Cron Job
      • Authentication
  • See also
  • Get Moped support, report a bug, or request an enhancement
  • Data & Technology Services
  • Github repository
Powered by GitBook
On this page
  • Pytest library
  • unittest.mock library
  • Test implementation
  • Extending the TestApp class
  • Testing AWS resources

Was this helpful?

  1. Dev Guides
  2. API

Testing

Libraries and patterns used to test the Moped API

PreviousConfiguration FilesNextUser Management API

Last updated 4 years ago

Was this helpful?

Pytest library

The framework is used to write and run the tests for the API.

unittest.mock library

The library is used to replace parts of code that are not relevant to a test. For example, for the protected routes in the User Management API, unit tests are written for the helpers that check for valid user attributes and user roles. After testing these helpers independently, there is no need to test them again in tests for the protected routes.

The return values of these checks can be patched using and/or the . These tools help tests focus on specific aspects of a method and avoid complicated logic that is unrelated to the test.

Test implementation

The base of the test implementation is the TestApp class that instantiates a test client that can be exposed within tests through self.client. The test client is an instance of the Client class documented . Through the client, tests can make requests to the Flask app as one would from it on a local or hosted server. More details can be found in the of the Moped API readme.

Extending the TestApp class

In order to create tests for different of the API, a new class that inherits TestApp can be created and written in a separate Python file. For example, to test the users blueprint, create a new file called test_users.py within the /tests/ folder. In that file, a new class called TestUsers can be defined.

class TestExamples(TestApp):
    def test_example(self):
        """Test that tests something."""
        result = test_something(arg)

        assert result is True

Testing AWS resources

An important pattern to successfully test AWS resources is mocking a resource, seeding it with any needed data, and then patching boto method calls with moto. As an example, to test a route or method that sends a request to a Cognito user pool, a test user pool with mock users must be initialized prior to patching the boto methods in that route. The patched boto methods will then interact with the mocked user pool for CRUD actions.

In order to test API routes that employ AWS resources, the library is used to mock AWS resources and isolate any side effects of the tests away from the staging or production AWS environments.

It is important to take the in order to avoid accessing live AWS resources from the test environment.

The easiest way to start tests with a mocked resource already initialized is with as documented in the . These fixtures allow individual tests to access a reusable and consistent mocked AWS resources and avoid repetition in tests that use the same AWS resources.

Pytest
unittest.mock
patchers
Mock class
Werkzeug
here
Test Driven Development section
Flask blueprints
moto
documented precautions
Pytest fixtures
moto usage example