Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing: ContentSwitcher behaving differently under testing #5132

Closed
kraanzu opened this issue Oct 16, 2024 · 9 comments
Closed

Testing: ContentSwitcher behaving differently under testing #5132

kraanzu opened this issue Oct 16, 2024 · 9 comments

Comments

@kraanzu
Copy link

kraanzu commented Oct 16, 2024

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!

Copy link

We found the following entries in the FAQ which you may find helpful:

Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review.

This is an automated reply, generated by FAQtory

@TomJGooding
Copy link
Contributor

TomJGooding commented Oct 16, 2024

I think you might just need to add an await to ensure the new content is mounted before your assert?

@kraanzu
Copy link
Author

kraanzu commented Oct 16, 2024

Hmm.. That worked. Pretty weird. I didn't think about this because all the places where I used this function, I did not use the await keyword and that works :)

@kraanzu kraanzu closed this as completed Oct 16, 2024
Copy link

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

@willmcgugan
Copy link
Collaborator

It will still work without the await, but it won't have completed on the very next line. Adding new content will do a little work in the background.

@kraanzu
Copy link
Author

kraanzu commented Oct 16, 2024

That makes a lot more sense. Thanks Will !

@kraanzu
Copy link
Author

kraanzu commented Oct 16, 2024

Question @willmcgugan , how should I test this then ideally?
For example, if I have a reactive variable that posts a message (OptionList's highlighted variable in this case) and I handle it by changing some portion in the UI, how do I verify that via tests?

Thank you!

@willmcgugan
Copy link
Collaborator

If you call await pilot.pause() it will wait for your messages to be processed. Then you can confirm the state is what you expect it to be.

@kraanzu
Copy link
Author

kraanzu commented Oct 16, 2024

That works. Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants