Masonite Documentation
v2.1
v2.1
  • Introduction and Installation
  • Creating a Blog
  • Prologue
    • Contributing Guide
    • How To Contribute
    • Release Cycle
    • Known Installation Issues
    • Deprecation
  • What's New
    • Masonite 1.3
    • Masonite 1.4
    • Masonite 1.5
    • Masonite 1.6
    • Masonite 2.0
    • Masonite 2.1
    • Masonite 2.2
  • Upgrade Guide
    • Masonite 1.3 to 1.4
    • Masonite 1.4 to 1.5
    • Masonite 1.5 to 1.6
    • Masonite 1.6 to 2.0
    • Masonite 2.0 to 2.1
  • The Basics
    • Controllers
    • Helper Functions
    • Requests
    • Routing
    • Static Files
    • Views
  • The Craft Command
    • Introduction
    • Creating Commands
  • Architectural Concepts
    • Request Lifecycle
    • Service Container
    • Service Providers
  • Advanced
    • Autoloading
    • Creating a Mail Driver
    • Creating Packages
    • Database Seeding
    • Extending Classes
    • Middleware
    • Responses
    • Sessions
    • Status Codes
    • Validation
  • Useful Features
    • Broadcasting
    • Caching
    • Compiling Assets
    • Environments
    • Events
    • Framework Hooks
    • Mail
    • Queues and Jobs
    • Task Scheduling
    • Testing
    • Template Caching
    • Uploading
    • View Composers, Sharing and Filters
  • Security
    • Authentication
    • CSRF Protection
    • Encryption
    • Headers
    • Releases
  • Orator ORM
    • Basic Usage
    • Collections
    • Database Migrations
    • ORM
    • Pagination
    • Query Builder
    • Schema Builder
  • Managers and Drivers
    • About Drivers
    • About Managers
    • Contracts
  • Official Packages
    • Masonite API
    • Masonite Billing
    • Masonite Dashboard
    • Masonite Notifications
  • Masonite Essentials
    • Hash ID's
  • Tutorials
    • Creating a Blog
  • How-to Guides
    • Build Email Verification from Scratch With Masonite Framework and JSON Web Tokens
    • Deploying a Masonite Application to Heroku
    • How To Deploy Masonite to PythonAnywhere
    • How-To: Use RabbitMQ with Masonite 2.0 queues
    • How To Use The Repository Pattern with Masonite
    • Making Masonite and Laravel Mix work together
  • Deployment
    • Drivers
    • Optimization
Powered by GitBook
On this page
  • Introduction
  • Configuration
  • Usage
  • Setup method
  • Routes
  • Status Code
  • JSON
  • Users
Edit on Git
Export as PDF
  1. Useful Features

Testing

Introduction

Masonite testing is very simple. You can test very complex parts of your code with ease by just extending your class with a Masonite unit test class.

You can run tests by running:

$ python -m pytest

Configuration

First, create a new test class in a testing directory. We will use the directory tests/unit for the purposes of this documentation. In that file we will create a test_unit.py file and put a simple class in it like so:

tests/unit/test_unit.py
class TestSomeUnit:
    pass

We will then inherit the Masonite UnitTest class so we have access to several built in helpers:

tests/unit/test_unit.py
from masonite.testing import UnitTest

class TestSomeUnit(UnitTest):
    pass

That's it! You're ready to start testing.

Your unit test needs to start with Test in order for pytest to pick it up.

Usage

Setup method

In unit testing, there is something called a setup_method. What this method does is it runs when the test class is first ran. This is good for setting up routes or anything else all your tests need.

If we modify the setup method, we need to call the parent classes setup method. This looks like this:

tests/unit/test_unit.py
from masonite.testing import UnitTest
from masonite.routes import Get

class TestSomeUnit(UnitTest):

    def setup_method(self):
        super().setup_method()

        self.routes([
            Get().route('/testing', 'TestController@show').name('testing.route').middleware('auth', 'owner')
        ])

Notice here we are adding a mock route here that we can do some testing on. You might instead want to test your routes specifically:

tests/unit/test_unit.py
from masonite.testing import UnitTest
from routes.web import ROUTES

class TestSomeUnit(UnitTest):

    def setup_method(self):
        super().setup_method()

        self.routes(ROUTES)

Now we are working with the routes in our specific project.

Routes

We have a few options for testing our routes.

Testing If a Route Exists:

tests/unit/test_unit.py
from masonite.testing import UnitTest
from routes.web import ROUTES

class TestSomeUnit(UnitTest):

    def setup_method(self):
        super().setup_method()

        self.routes([
            Get().route('/testing', 'SomeController@show').name('testing.route').middleware('auth', 'owner')
        ])

    def test_route_exists(self):
        assert self.route('/testing')

Testing If Route Has The Correct Name

tests/unit/test_unit.py
def test_route_has_the_correct_name(self):
    assert self.route('/testing').is_named('testing.route')

Testing If A Route Has The Correct Middleware

tests/unit/test_unit.py
def test_route_has_route_middleware(self):
    assert self.route('/testing').has_middleware('auth', 'owner')

Testing If A Route Has The Correct Controller

tests/unit/test_unit.py
from app.http.controllers.SomeController import SomeController

def test_unit_test_has_controller(self):
    assert self.route('/testing').has_controller(SomeController)

Testing If A Route Contains A String

This can be used to see if the template returned a specific value

tests/unit/test_unit.py
def test_view_contains(self):
    assert self.route('/testing').contains('Login')

Status Code

You can also check if a route is "ok" or a status 200:

tests/unit/test_unit.py
def test_view_is_status_200(self):
    assert self.route('/testing').status('200 OK')

or a shorthand:

tests/unit/test_unit.py
def test_view_is_ok(self):
    assert self.route('/testing').ok()

JSON

You can also test the result of a JSON response:

tests/unit/test_unit.py
def test_json_response(self):
    json = self.json('/test/json/response/1', {'id': 1}, method="POST")
    assert json.status('200 OK')
    assert json.contains('success')

Users

We can load users into the route and check if they can view the route. This is good to see if your middleware is acting good against various users.

For example we can check if a user that isn't logged in has access to the dashboard homepage:

tests/unit/test_unit.py
def test_guest_user_can_view(self):
    assert not self.route('/some/protect/route').user(None).can_view()

Or we can set a value on a mock user and see if that passes:

tests/unit/test_unit.py
class MockUser:
    is_admin = 1

def test_owner_user_can_view(self):
    assert self.route('/some/protect/route').user(MockUser).can_view()
PreviousTask SchedulingNextTemplate Caching

Last updated 6 years ago