Skip to content

Testing: ContentSwitcher behaving differently under testing  #5132

@kraanzu

Description

@kraanzu

Have you checked closed issues? https://github.com/Textualize/textual/issues?q=is%3Aissue+is%3Aclosed

Yes

Please give a brief but clear explanation of the issue. If you can, include a complete working example that demonstrates the bug. Check it can run without modifications.

Thanks for the built in testing mechanism! I've noticed some weird behaviour while testing with ContentSwitcher

Take this example for instance:

from textual.app import App, ComposeResult
from textual.widgets import ContentSwitcher, Label


class MyApp(App):
    def compose(self) -> ComposeResult:
        self.c = ContentSwitcher()
        yield self.c


async def test_basic():
    async with MyApp().run_test() as pilot:
        app = pilot.app

        assert isinstance(app, MyApp)

        app.c.add_content(Label("Hello", id="test"), set_current=True)

        assert app.c.visible_content.id == "test"

When run, this throws an error:

::test_basic - AttributeError: 'NoneType' object has no attribute 'id'

which means, it doesn't even get mounted
Note: If I pass an initial parameter, that gets recognized

from textual.app import App, ComposeResult
from textual.widgets import ContentSwitcher, Label


class MyApp(App):
    def compose(self) -> ComposeResult:
        self.c = ContentSwitcher(Label("Default", id="default"), initial = "default")
        yield self.c


async def test_basic():
    async with MyApp().run_test() as pilot:
        app = pilot.app

        assert isinstance(app, MyApp)

        app.c.add_content(Label("Hello", id="test"), set_current=True)

        assert app.c.visible_content.id == "default"

It will be helpful if you run the following command and paste the results:

textual diagnose

Textual Diagnostics

Versions

Name Value
Textual 0.83.0
Rich 13.9.2

Python

Name Value
Version 3.12.5
Implementation CPython
Compiler GCC 13.3.0
Executable /home/kraanzu/Projects/dooit/.venv/bin/python3

Operating System

Name Value
System Linux
Release 6.6.54
Version #1-NixOS SMP PREEMPT_DYNAMIC Fri Oct 4 14:30:05 UTC 2024

Terminal

Name Value
Terminal Application WezTerm (20240203-110809-5046fc22)
TERM xterm-256color
COLORTERM truecolor
FORCE_COLOR Not set
NO_COLOR Not set

Rich Console options

Name Value
size width=145, height=36
legacy_windows False
min_width 1
max_width 145
is_terminal True
encoding utf-8
max_height 36
justify None
overflow None
no_wrap False
highlight None
markup None
height None

If you don't have the textual command on your path, you may have forgotten to install the textual-dev package.

Feel free to add screenshots and / or videos. These can be very helpful!

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