Closed MalayAgr closed 1 year ago
Update: Since then, I've implemented keyboard support for the number buttons and the test passes if I use pilot.press()
instead of pilot.click()
. This is the testing code with pilot.press()
:
async def test_number_buttons():
async with CalculatorApp().run_test() as pilot:
await pilot.press("1")
display_content = pilot.app.query_one("#viewport", Static).render()
assert str(display_content) == "1"
await pilot.press("2")
display_content = pilot.app.query_one("#viewport", Static).render()
assert str(display_content) == "12"
await pilot.press("3")
display_content = pilot.app.query_one("#viewport", Static).render()
assert str(display_content) == "123"
Update: Since then, I've realized that it is possible to access class attributes like viewport
, show_ac
, left
, right
, etc. I've tried two variants of the above test with this in mind.
This one uses app.value
.
async def test_number_buttons():
async with CalculatorApp().run_test() as pilot:
await pilot.click("#number-1")
assert str(pilot.app.value) == "1"
await pilot.click("#number-2")
assert str(pilot.app.value) == "12"
await pilot.click("#number-3")
assert str(pilot.app.value) == "123"
This one uses app.viewport
.
async def test_number_buttons():
async with CalculatorApp().run_test() as pilot:
await pilot.click("#number-1")
assert str(pilot.app.viewport) == "1"
await pilot.click("#number-2")
assert str(pilot.app.viewport) == "12"
await pilot.click("#number-3")
assert str(pilot.app.viewport) == "123"
Both of them fail because app.value
is always ""
and app.viewport
is always "0"
, even though the GUI gets updated correctly.
Both of them fail because app.value is always "" and app.viewport is always "0", even though the GUI gets updated correctly.
At first glance this sort of makes sense, when running with the pilot. What is happening is that the call to click
is generating fake mouse messages (MouseDown
, MouseUp
and Click
) and then returning after a pause
. Those messages are going to then eventually have to result in a ButtonClicked
message, which in turn will result in on_button_pressed
being invoked, which in turn will result in value
being changed, which in turn will eventually result in the Static
that shows the numbers rendering the updated value.
Point being: by the time you get to the first assert
most of that work won't have taken place yet.
However, it's reasonable to assume that the pilot.click
should come back once the work has been done, so we'll dive a bit deeper into this and see if we can make that work.
Right, found the issue and it's not what we were initially thinking. Fix is about to get under way.
Thanks for raising this; it's a good spot!
Don't forget to star the repository!
Follow @textualizeio for Textual updates.
Just to update, this fix is now available: https://github.com/Textualize/textual/releases/tag/v0.15.0
Thanks!
Hi,
I am trying to write tests for the example calculator app present in the repo (at the same time, expanding it to add a few more operators). This is my
CalculatorApp
class:I wrote the following test to check that clicking the number buttons results in the calculator's display (
Static(id="viewport")
) accumulating digits to make a number:While the GUI gets updated correctly on clicking the buttons, the test always fails since
app.query_one("#viewport").render()
always returns"0"
. I've also tried replacingapp.query_one("#viewport").render()
withapp.query_one("#viewport", Static).render()
but that hasn't helped either.Is this supposed to happen?
Textual Diagnostics
Versions
Python
Operating System
Terminal
Rich Console options