Skip to content

Commit

Permalink
Factored out a common app fixture.
Browse files Browse the repository at this point in the history
  • Loading branch information
freakboy3742 committed Aug 12, 2023
1 parent e0a46dc commit fec495d
Show file tree
Hide file tree
Showing 14 changed files with 37 additions and 95 deletions.
7 changes: 1 addition & 6 deletions core/tests/app/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@
}


@pytest.fixture
def app():
return toga.App(formal_name="Test App", app_id="org.example.test")


@pytest.mark.parametrize(
(
"kwargs, metadata, main_module, expected_formal_name, expected_app_id, expected_app_name, "
Expand Down Expand Up @@ -399,7 +394,7 @@ def test_icon(app, construct):

# Default icon matches app name
assert isinstance(app.icon, toga.Icon)
assert app.icon.path == "resources/test"
assert app.icon.path == "resources/test_app"

# Change icon
app.icon = icon
Expand Down
5 changes: 0 additions & 5 deletions core/tests/app/test_mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
from toga_dummy.utils import assert_action_performed


@pytest.fixture
def app():
return toga.App("Test App", "org.beeware.toga.app.main_window")


def test_create(app):
"A MainWindow can be created with minimal arguments"
window = toga.MainWindow()
Expand Down
5 changes: 0 additions & 5 deletions core/tests/app/test_windowset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
from toga.app import WindowSet


@pytest.fixture
def app():
return toga.App("Test App", "org.beeware.toga.app.main_window")


@pytest.fixture
def window1():
return toga.Window(title="Window 1")
Expand Down
5 changes: 0 additions & 5 deletions core/tests/command/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ def assert_order(*items):
assert not items[i] > 42


@pytest.fixture
def app():
return toga.App("Command Test", "org.beeware.command")


def test_break():
"""A break can be created"""

Expand Down
5 changes: 0 additions & 5 deletions core/tests/command/test_commandset.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@
from toga.command import GROUP_BREAK, SECTION_BREAK, CommandSet


@pytest.fixture
def app():
return toga.App("CommandSet Test", "org.beeware.commandset")


def test_create():
"""A CommandSet can be created with defaults"""
cs = CommandSet()
Expand Down
19 changes: 19 additions & 0 deletions core/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
import sys

import pytest

import toga
from toga_dummy.utils import EventLog


@pytest.fixture(autouse=True)
def reset_event_log():
EventLog.reset()


@pytest.fixture(autouse=True)
def clear_sys_modules(monkeypatch):
try:
# App startup is influenced by things like the state of sys.modules, and the
# presence of __main__ in particular. Pytest doesn't need __main__ to work;
# so if it exists, delete it for the purposes of each test.
monkeypatch.delitem(sys.modules, "__main__")
except KeyError:
pass


@pytest.fixture
def app(event_loop):
return toga.App(formal_name="Test App", app_id="org.beeware.toga.test-app")
5 changes: 0 additions & 5 deletions core/tests/test_documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ def read(self):
pass


@pytest.fixture
def app():
return toga.App("Document Test", "org.beeware.toga.documents")


@pytest.mark.parametrize("path", ["/path/to/doc.mydoc", Path("/path/to/doc.mydoc")])
def test_create_document(app, path):
doc = MyDoc(path, app)
Expand Down
5 changes: 0 additions & 5 deletions core/tests/test_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@
ABSOLUTE_FILE_PATH = Path(toga.__file__).parent / "resources" / "toga.png"


@pytest.fixture
def app():
return toga.App("Images Test", "org.beeware.toga.images")


@pytest.mark.parametrize(
"args, kwargs",
[
Expand Down
7 changes: 1 addition & 6 deletions core/tests/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@
)


@pytest.fixture
def app(event_loop):
return toga.App("Test App", "org.beeware.toga.window")


@pytest.fixture
def window():
return toga.Window()
Expand Down Expand Up @@ -83,7 +78,7 @@ def test_set_app(window, app):

assert window.app == app

app2 = toga.App("Test App 2", "org.beeware.toga.window2")
app2 = toga.App("Test App 2", "org.beeware.toga.test-app-2")
with pytest.raises(ValueError, match=r"Window is already associated with an App"):
window.app = app2

Expand Down
49 changes: 16 additions & 33 deletions core/tests/widgets/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,9 @@ def test_add_child_without_app(widget):
assert_action_performed_with(widget, "refresh")


def test_add_child(widget):
def test_add_child(app, widget):
"A child can be added to a node when there's an app & window"
# Set the app and window for the widget.
app = toga.App("Test", "com.example.test")
window = toga.Window()
window.app = app
window.content = widget
Expand Down Expand Up @@ -160,10 +159,9 @@ def test_add_child(widget):
assert app.widgets["child_id"] == child


def test_add_multiple_children(widget):
def test_add_multiple_children(app, widget):
"Multiple children can be added in one call"
# Set the app and window for the widget.
app = toga.App("Test", "com.example.test")
window = toga.Window()
window.app = app
window.content = widget
Expand Down Expand Up @@ -333,10 +331,9 @@ def test_insert_child_without_app(widget):
assert_action_performed_with(widget, "refresh")


def test_insert_child(widget):
def test_insert_child(app, widget):
"A child can be inserted into a node when there's an app & window"
# Set the app and window for the widget.
app = toga.App("Test", "com.example.test")
window = toga.Window()
window.app = app
window.content = widget
Expand Down Expand Up @@ -383,10 +380,9 @@ def test_insert_child(widget):
assert app.widgets["child_id"] == child


def test_insert_position(widget):
def test_insert_position(app, widget):
"Insert can put a child into a specific position"
# Set the app and window for the widget.
app = toga.App("Test", "com.example.test")
window = toga.Window()
window.app = app
window.content = widget
Expand Down Expand Up @@ -451,10 +447,9 @@ def test_insert_position(widget):
assert app.widgets["child3_id"] == child3


def test_insert_bad_position(widget):
def test_insert_bad_position(app, widget):
"If the position is invalid, an error is raised"
# Set the app and window for the widget.
app = toga.App("Test", "com.example.test")
window = toga.Window()
window.app = app
window.content = widget
Expand Down Expand Up @@ -588,13 +583,12 @@ def test_remove_child_without_app(widget):
assert_action_performed_with(widget, "refresh")


def test_remove_child(widget):
def test_remove_child(app, widget):
"A child associated with an app & window can be removed from a widget"
# Add a child to the widget
child = ExampleLeafWidget(id="child_id")
widget.add(child)

app = toga.App("Test", "com.example.test")
window = toga.Window()
window.app = app
window.content = widget
Expand Down Expand Up @@ -627,15 +621,14 @@ def test_remove_child(widget):
assert_action_performed_with(window.content, "refresh")


def test_remove_multiple_children(widget):
def test_remove_multiple_children(app, widget):
"Multiple children can be removed from a widget"
# Add children to the widget
child1 = ExampleLeafWidget(id="child1_id")
child2 = ExampleLeafWidget(id="child2_id")
child3 = ExampleLeafWidget(id="child3_id")
widget.add(child1, child2, child3)

app = toga.App("Test", "com.example.test")
window = toga.Window()
window.app = app
window.content = widget
Expand Down Expand Up @@ -678,15 +671,14 @@ def test_remove_multiple_children(widget):
assert_action_performed_with(window.content, "refresh")


def test_clear_all_children(widget):
def test_clear_all_children(app, widget):
"All children can be simultaneously removed from a widget"
# Add children to the widget
child1 = ExampleLeafWidget(id="child1_id")
child2 = ExampleLeafWidget(id="child2_id")
child3 = ExampleLeafWidget(id="child3_id")
widget.add(child1, child2, child3)

app = toga.App("Test", "com.example.test")
window = toga.Window()
window.app = app
window.content = widget
Expand Down Expand Up @@ -730,9 +722,8 @@ def test_clear_all_children(widget):
assert_action_performed_with(window.content, "refresh")


def test_clear_no_children(widget):
def test_clear_no_children(app, widget):
"No changes are made (no-op) if widget has no children"
app = toga.App("Test", "com.example.test")
window = toga.Window()
window.app = app
window.content = widget
Expand All @@ -754,10 +745,9 @@ def test_clear_no_children(widget):
assert_action_not_performed(window.content, "refresh")


def test_clear_leaf_node():
def test_clear_leaf_node(app):
"No changes are made to leaf node that cannot have children"
leaf = ExampleLeafWidget()
app = toga.App("Test", "com.example.test")
window = toga.Window()
window.app = app
window.content = leaf
Expand Down Expand Up @@ -805,9 +795,8 @@ def test_remove_from_non_parent(widget):
assert_action_not_performed(widget, "refresh")


def test_set_app(widget):
def test_set_app(app, widget):
"A widget can be assigned to an app"
app = toga.App("Test App", "org.beeware.test")
assert len(app.widgets) == 0

# Assign the widget to an app
Expand All @@ -824,16 +813,14 @@ def test_set_app(widget):
assert attribute_value(widget, "app") == app


def test_set_app_with_children(widget):
def test_set_app_with_children(app, widget):
"If a widget has children, the children get the app assignment"
# Add children to the widget
child1 = ExampleLeafWidget(id="child1_id")
child2 = ExampleLeafWidget(id="child2_id")
child3 = ExampleLeafWidget(id="child3_id")
widget.add(child1, child2, child3)

# Set up an app
app = toga.App("Test App", "org.beeware.test")
assert len(app.widgets) == 0

# Assign the widget to an app
Expand Down Expand Up @@ -861,9 +848,8 @@ def test_set_app_with_children(widget):
assert attribute_value(child3, "app") == app


def test_set_same_app(widget):
def test_set_same_app(app, widget):
"A widget can be re-assigned to the same app"
app = toga.App("Test App", "org.beeware.test")
assert len(app.widgets) == 0

# Assign the widget to an app
Expand All @@ -879,9 +865,8 @@ def test_set_same_app(widget):
assert_attribute_not_set(widget, "app")


def test_reset_app(widget):
def test_reset_app(app, widget):
"A widget can be re-assigned to no app"
app = toga.App("Test App", "org.beeware.test")
assert len(app.widgets) == 0

# Assign the widget to an app
Expand All @@ -903,10 +888,8 @@ def test_reset_app(widget):
assert attribute_value(widget, "app") is None


def test_set_new_app(widget):
def test_set_new_app(app, widget):
"A widget can be assigned to a different app"
app = toga.App("Test App", "org.beeware.test")

# Assign the widget to an app
widget.app = app
assert len(app.widgets) == 1
Expand All @@ -915,7 +898,7 @@ def test_set_new_app(widget):
EventLog.reset()

# Create a new app
new_app = toga.App("Test App", "org.beeware.test")
new_app = toga.App("Test App", "org.beeware.toga.test-app")
assert len(new_app.widgets) == 0

# Assign the widget to the same app
Expand Down
5 changes: 0 additions & 5 deletions core/tests/widgets/test_imageview.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@
)


@pytest.fixture
def app():
return toga.App("ImageView Test", "org.beeware.toga.widgets.imageview")


@pytest.fixture
def widget(app):
return toga.ImageView()
Expand Down
5 changes: 0 additions & 5 deletions core/tests/widgets/test_optioncontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@
)


@pytest.fixture
def app():
return toga.App("Option Container Test", "org.beeware.toga.option_container")


@pytest.fixture
def window():
return toga.Window()
Expand Down
5 changes: 0 additions & 5 deletions core/tests/widgets/test_scrollcontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@
)


@pytest.fixture
def app():
return toga.App("Scroll Container Test", "org.beeware.toga.scroll_container")


@pytest.fixture
def window():
return toga.Window()
Expand Down
5 changes: 0 additions & 5 deletions core/tests/widgets/test_splitcontainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@
)


@pytest.fixture
def app():
return toga.App("Split Container Test", "org.beeware.toga.split_container")


@pytest.fixture
def window():
return toga.Window()
Expand Down

0 comments on commit fec495d

Please sign in to comment.