Skip to content

Test Failure On Windows #566

@HassanAbouelela

Description

@HassanAbouelela

Description

The current unittests fail on windows, when using the default test command:

python manage.py test --no-input

Cause

The following is the failing test: pydis_site.apps.content.tests.test_views.test_get_context_data_breadcrumbs

def test_get_context_data_breadcrumbs(self):
"""The method should return correct breadcrumbs."""
request = self.factory.get("/category/subcategory/with_metadata")
self.ViewClass.setup(request)
self.ViewClass.dispatch(request, location="category/subcategory/with_metadata")
context = self.ViewClass.get_context_data()
self.assertEquals(
context["breadcrumb_items"],
[
{"name": PARSED_CATEGORY_INFO["title"], "path": "."},
{"name": PARSED_CATEGORY_INFO["title"], "path": "category"},
{"name": PARSED_CATEGORY_INFO["title"], "path": "category/subcategory"},
]
)

The reason is in the following line:

{"name": PARSED_CATEGORY_INFO["title"], "path": "category/subcategory"},

Now, context["breadcrumb_items"][2]["path"] would normally produce category/subcategory. Those who have worked with paths before know where this is going, but since we are doing a blind comparison on the string values of paths, we end up with the following assertion error:

category/subcategory != category\\subcategory
(left is expected, right is the output on windows)

Solution

These sort of things wouldn't cause actual problems while running, because windows, and python, can both gracefully handle either format.

One option when it comes to fixing this test, is to pass both ends through a Path call, resulting in:

        context = self.ViewClass.get_context_data()
        context["breadcrumb_items"][2]["path"] = Path(context["breadcrumb_items"][2]["path"])

        self.assertEquals(
            context["breadcrumb_items"],
            [
                {"name": PARSED_CATEGORY_INFO["title"], "path": "."},
                {"name": PARSED_CATEGORY_INFO["title"], "path": "category"},
                {"name": PARSED_CATEGORY_INFO["title"], "path": Path("category/subcategory")},
            ]
        )

This should work because we will be comparing two paths objects, instead of pure strings.

This isn't a real solution though, generally speaking. It will solve the problem here, but this sort of thing can slip in at any time. This specific bug appears to have existed since Kosa did the rewrite, but I'm willing to bet it has existed for much longer. Either way, it's been in for quite a while.

Now, one thing we could do is add windows to our testing CI. It's not a fun process from my experience, but I can't think of a better solution right now. Alternatively, we just completely ignore windows, though as a windows user, I have to try and advocate against that :).

I'm open to hearing other solutions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: testsRelated to `unittest` teststype: bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions