Skip to content

Add more tests #110

Open
Open

Description

@matrixise

We can't count on the current coverage of the tests, currently, it's not enough. We have to improve it because, without that, the deployment is harder.

Activity

  1. matrixise commented on Nov 21, 2025

    @matrixise
    ContributorAuthor

    Here's a proposed approach to improve test coverage:

    1. Measure current coverage

    pip install coverage
    coverage run pythonie/manage.py test pythonie --settings=pythonie.settings.tests
    coverage report --show-missing

    2. Priority by app

    App What to test
    core HomePage, SimplePage, StreamField blocks, PageSegment snippets
    sponsors Sponsor, SponsorshipLevel, relationships
    speakers Speaker, Session, Sessionize import
    meetups Meetup model, updatemeetups command, API sync

    3. Types of tests to add

    Models (unit tests)

    class SponsorTest(TestCase):
        def test_sponsor_str(self):
            sponsor = Sponsor(name="Acme Corp")
            self.assertEqual(str(sponsor), "Acme Corp")

    Wagtail pages (using WagtailPageTestCase)

    from wagtail.test.utils import WagtailPageTestCase
    
    class HomePageTest(WagtailPageTestCase):
        def test_can_create_homepage(self):
            self.assertCanCreateAt(Page, HomePage)

    Management commands

    from django.core.management import call_command
    from unittest.mock import patch
    from io import StringIO
    
    class UpdateMeetupsTest(TestCase):
        @patch('meetups.management.commands.updatemeetups.requests.get')
        def test_updatemeetups_command(self, mock_get):
            mock_get.return_value.json.return_value = []
            out = StringIO()
            call_command('updatemeetups', stdout=out)

    Views/URLs

    class SponsorPageTest(TestCase):
        def test_sponsors_page_loads(self):
            response = self.client.get('/sponsors/')
            self.assertEqual(response.status_code, 200)

    4. Recommended structure

    pythonie/
    ├── core/
    │   └── tests/
    │       ├── __init__.py
    │       ├── test_models.py
    │       └── test_pages.py
    ├── sponsors/
    │   └── tests/
    │       ├── __init__.py
    │       └── test_models.py
    ...
    

    5. Use FactoryBoy (related to #81)

    Adding factory-boy would help both test coverage and sample data generation:

    import factory
    from sponsors.models import Sponsor, SponsorshipLevel
    
    class SponsorshipLevelFactory(factory.django.DjangoModelFactory):
        class Meta:
            model = SponsorshipLevel
        name = factory.Sequence(lambda n: f"Level {n}")
    
    class SponsorFactory(factory.django.DjangoModelFactory):
        class Meta:
            model = Sponsor
        name = factory.Faker('company')
        level = factory.SubFactory(SponsorshipLevelFactory)

    If someone wants to pick this up and create a branch, contributions are welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions