Context:
- I am developing a standalone Django package that is intended to be added to existing Django projects.
- This package includes Django models with methods that interact directly with a database.
- I want to write tests for these methods using
pytest
andpytest-django
. - I need to run these tests in a CI/CD environment, specifically using GitHub Actions.
Problems Encountered:
-
Database Setup:
- Since this is a standalone package, it does not include a
manage.py
file. - This makes it challenging to run typical Django management commands like
migrate
to set up the database schema for testing.
- Since this is a standalone package, it does not include a
-
Table Creation:
- I encounter errors indicating that the table for my model does not exist because the necessary migrations are not applied.
- This is problematic because my tests need to interact with the database tables directly.
-
Fixtures and Setup:
- I need to ensure the database tables are created and populated with initial data before the tests run.
- My attempts to use
pytest
fixtures and manually create database tables within the tests have not been successful.
What I’ve Tried:
- Using
pytest
fixtures to set up the initial database state. - Manually creating database tables within the test setup using direct SQL execution.
- Using the
mixer
library to populate the database with test data.
Desired Outcome:
- A reliable method to ensure the necessary database tables for my models are created before running the tests.
- Proper setup and teardown of test data within the standalone package.
- Integration with CI/CD pipelines like GitHub Actions without relying on
manage.py
commands.
Additional Information:
- I am using an in-memory SQLite database for faster test execution.
- The test environment should be isolated and not rely on external databases or services.
I am looking for guidance on how to approach this problem or best practices for testing Django models in a standalone package using pytest
and pytest-django
. Any advice or solutions would be greatly appreciated.
Alex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
You don’t need manage.py
to run migrate
. The same commands are available via the django-admin
command that Django installs, but you’ll need to set the DJANGO_SETTINGS_MODULE
variable that manage.py
would ordinarily set.
IOW, if you’d generally run python manage.py
and your project settings are myproject.settings
, you can just as well run env DJANGO_SETTINGS_MODULE=myproject.settings django-admin migrate
.
That said, that’s not your issue here – pytest-django will deal with setting up and tearing down a testing database for you. Generally, when developing a Django package, you’d have a testing project and app next to it to host the package for development use.
An example (of a standalone package, tested with GitHub Actions) is here (repository maintained by yours truly); lippukala
is the reusable package, and lippukala_test_app
is the package that hosts the settings for pytest-django
to use; there’s a stanza
[tool.pytest.ini_options]
DJANGO_SETTINGS_MODULE = "lippukala_test_app.settings"
in the pyproject.toml file that makes that work.
2