How to test Django models against a DB without manage.py in a standalone package using pytest and GitHub Actions?

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 and pytest-django.
  • I need to run these tests in a CI/CD environment, specifically using GitHub Actions.

Problems Encountered:

  1. 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.
  2. 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.
  3. 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.

New contributor

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật