Masonite Documentation
v4.0
v4.0
  • Introduction and Installation
  • Prologue
    • Creating A Blog Tutorial
    • Release Cycle
    • Contributing Guide
    • How To Contribute
  • The Basics
    • Routing
    • Controllers
    • Middleware
    • Response
    • Request
    • Static Files
    • Views
    • Environments
    • Configuration
    • Error Handling
  • Features
    • API Development
    • Authentication
    • Authorization
    • Broadcasting
    • Caching
    • Compiling Assets
    • Commands
    • CSRF Protection
    • Events
    • Facades
    • Filesystem and Uploading
    • Hash ID's
    • Helpers
    • Mail
    • Notifications
    • Package Development
    • Queues and Jobs
    • Rate Limiting
    • Sessions
    • Task Scheduling
    • Tinker Shell (REPL)
    • Validation
  • Architecture
    • Service Providers
    • Service Container
  • Security
    • CORS
    • Hashing
  • Masonite ORM
    • To Masonite ORM Docs
  • Testing
    • Getting Started
    • HTTP Tests
    • Database Tests
    • Commands Tests
    • Console Tests
    • Mocking
    • Extending
  • Official Packages
    • Masonite Debugbar
  • How-to Guides
    • Handling AJAX requests with expired authentication
    • 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
  • What's New
    • Masonite 1.3
    • Masonite 1.4
    • Masonite 1.5
    • Masonite 1.6
    • Masonite 2.0
    • Masonite 2.1
    • Masonite 2.2
    • Masonite 2.3
    • Masonite 3.0
  • 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
    • Masonite 2.1 to 2.2
    • Masonite 2.2 to 2.3
    • Masonite 2.3 to 3.0
    • Masonite 3.0 to 4.0
Powered by GitBook
On this page
  • Resetting The Database After Each Test
  • Available Assertions
Edit on GitHub
Export as PDF
  1. Testing

Database Tests

By default, your tests are are not ran in isolation from a database point of view. It means that your local database will be modified any time you run your tests and won't be rollbacked at the end of the tests. While this behaviour might be fine in most case you can learn below how to configure your tests cases to reset the database after each test.

Resetting The Database After Each Test

If you want to have a clean database for each test you must subclass the TestCase class with DatabaseTransactions class. Then all your tests will run inside a transaction so any data you create will only exist within the lifecycle of the test. Once the test completes, your database is rolled back to its previous state. This is a perfect way to prevent test data from clogging up your database.

from masonite.tests import TestCase, DatabaseTransactions

class TestSomething(TestCase, DatabaseTransactions):

  connection = "testing"

  def test_can_create_user(self):
      User.create({"name": "john", "email": "john6", "password": "secret"})

Note that you can define the connection that will be used during testing. This will allow you to select a different database that will be used for testing. Here is a standard exemple of database configuration file that you can use.

# config/database.py
DATABASES = {
    "default": "mysql",
    "mysql": {
        "host": "localhost",
        "driver": "mysql",
        "database": "app",
        "user": "root",
        "password": "",
        "port": 3306
    }
    "testing": {
        "driver": "sqlite",
        "database": "test_database.sqlite3",
    },
}

Available Assertions

Masonite provides several database assertions that can be used during testing.

assertDatabaseCount

Assert that a table in the database contains the given number of records.

self.assertDatabaseCount(table, count)
  def test_can_create_user(self):
      User.create({"name": "john", "email": "john6", "password": "secret"})
      self.assertDatabaseCount("users", 1)

assertDatabaseHas

Assert that a table in the database contains records matching the given query.

self.assertDatabaseHas(table, query_dict)
self.assertDatabaseCount("users", {"name": "John"})

assertDatabaseMissing

Assert that a table in the database does not contain records matching the given query.

self.assertDatabaseMissing(table, query_dict)
self.assertDatabaseMissing("users", {"name": "Jack"})

assertDeleted

Assert that the given model instance has been deleted from the database.

user=User.find(1)
user.delete()
self.assertDeleted(user)

assertSoftDeleted

self.assertSoftDeleted(user)
PreviousHTTP TestsNextCommands Tests

Last updated 3 years ago

Assert that the given model instance has been from the database.

soft deleted
assertDatabaseCount
assertDatabaseHas
assertDatabaseMissing
assertDeleted
assertSoftDeleted