From 596c1ebfcc66bed8749f224a22eda28e1f80c446 Mon Sep 17 00:00:00 2001 From: u8slvn Date: Sat, 31 Aug 2024 13:29:33 +0200 Subject: [PATCH] test: improve test_world --- README.md | 2 +- tests/conftest.py | 6 +++--- tests/dog/test_body.py | 8 ++++---- tests/dog/test_dog.py | 16 +++++++++------- tests/test_prepare.py | 4 ++-- tests/test_world.py | 3 ++- 6 files changed, 21 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 813bbd6..443edf8 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@

- Until Zero - タイマー | logo + doggo logo

diff --git a/tests/conftest.py b/tests/conftest.py index dc2d3da..77520cd 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4,9 +4,9 @@ import pytest -@pytest.fixture(scope="session", autouse=True) -def pygame(): - """Start and stop pygame instance for all tests.""" +@pytest.fixture(scope="session") +def pygame_test(): + """Start and stop pygame instance for a test.""" pg.init() pg.display.set_mode((1, 1)) diff --git a/tests/dog/test_body.py b/tests/dog/test_body.py index 99b0947..a86bcd6 100644 --- a/tests/dog/test_body.py +++ b/tests/dog/test_body.py @@ -19,7 +19,7 @@ def test_fur_random_returns_a_fur_color(): assert isinstance(fur, Fur) -def test_body_get_image_returns_surface_cyclically(): +def test_body_get_image_returns_surface_cyclically(pygame_test): body = Body( fur=Fur.random(), sprite_size=config.SPRITE_SIZE, @@ -45,7 +45,7 @@ def test_body_get_image_returns_surface_cyclically(): assert next_cycle_image is frames[0] -def test_body_return_cycle_images_while_brain_state_is_the_same(): +def test_body_return_cycle_images_while_brain_state_is_the_same(pygame_test): body = Body( fur=Fur.random(), sprite_size=config.SPRITE_SIZE, @@ -74,7 +74,7 @@ def test_body_return_cycle_images_while_brain_state_is_the_same(): assert fourth_image is body.images[StateID.IDLE][Direction.RIGHT][1] -def test_sprite_sheet_get_sprite_returns_a_sprite(): +def test_sprite_sheet_get_sprite_returns_a_sprite(pygame_test): sprite_sheet_path = ASSETS_PATH.joinpath("dogs/00.png") sprite_sheet = SpriteSheet(path=sprite_sheet_path, columns=8, rows=9) @@ -85,7 +85,7 @@ def test_sprite_sheet_get_sprite_returns_a_sprite(): assert sprite.get_height() == 48 -def test_sprite_sheet_get_sprite_fails_if_loc_is_outside_of_the_sheet(): +def test_sprite_sheet_get_sprite_fails_if_loc_is_outside_of_the_sheet(pygame_test): sprite_sheet_path = ASSETS_PATH.joinpath("dogs/00.png") sprite_sheet = SpriteSheet(path=sprite_sheet_path, columns=8, rows=9) diff --git a/tests/dog/test_dog.py b/tests/dog/test_dog.py index 85d47f5..e5789dc 100644 --- a/tests/dog/test_dog.py +++ b/tests/dog/test_dog.py @@ -7,7 +7,7 @@ from doggo.prepare import build_dog -def test_dog_can_move_right(): +def test_dog_can_move_right(pygame_test): dog = build_dog() dog.brain.change_state(StateID.WALK) # We need a state that can move. dog.brain.current_state.direction = Direction.RIGHT @@ -18,7 +18,7 @@ def test_dog_can_move_right(): assert dog.rect.x > start_x_pos -def test_dog_can_move_left(): +def test_dog_can_move_left(pygame_test): dog = build_dog() dog.brain.change_state(StateID.WALK) # We need a state that can move. dog.brain.current_state.direction = Direction.LEFT @@ -37,7 +37,7 @@ def test_dog_can_move_left(): ], ) def test_dog_clone_appeared_when_hitting_a_world_boundary( - direction, start_pos, x_signe + pygame_test, direction, start_pos, x_signe ): dog = build_dog() dog.brain.change_state(StateID.WALK) # We need a state that can move. @@ -61,7 +61,9 @@ def test_dog_clone_appeared_when_hitting_a_world_boundary( (Direction.LEFT, lambda dog: 0), ], ) -def test_dog_clone_disappeared_when_not_hitting_a_world_boundary(direction, start_pos): +def test_dog_clone_disappeared_when_not_hitting_a_world_boundary( + pygame_test, direction, start_pos +): dog = build_dog() dog.brain.change_state(StateID.WALK) dog.brain.current_state.direction = direction @@ -83,7 +85,7 @@ def test_dog_clone_disappeared_when_not_hitting_a_world_boundary(direction, star assert dog.clone.visible is False -def test_dog_draw_on_screen(pg_screen_mock): +def test_dog_draw_on_screen(pygame_test, pg_screen_mock): dog = build_dog() dog.draw(screen=pg_screen_mock) @@ -91,7 +93,7 @@ def test_dog_draw_on_screen(pg_screen_mock): pg_screen_mock.blit.assert_called_once_with(dog.image, dest=dog.rect) -def test_dog_draw_on_screen_with_its_clone(mocker, pg_screen_mock): +def test_dog_draw_on_screen_with_its_clone(mocker, pygame_test, pg_screen_mock): dog = build_dog() dog.clone.visible = True @@ -104,7 +106,7 @@ def test_dog_draw_on_screen_with_its_clone(mocker, pg_screen_mock): ] -def test_dog_brain_info_are_accessible_from_dog(): +def test_dog_brain_info_are_accessible_from_dog(pygame_test): dog = build_dog() assert isinstance(dog.current_state, StateID) diff --git a/tests/test_prepare.py b/tests/test_prepare.py index a059b61..ffdeea3 100644 --- a/tests/test_prepare.py +++ b/tests/test_prepare.py @@ -10,7 +10,7 @@ from doggo.prepare import build_landscape -def test_build_dog(): +def test_build_dog(pygame_test): dog = build_dog() assert isinstance(dog, Dog) @@ -19,7 +19,7 @@ def test_build_dog(): assert isinstance(dog.body, Body) -def test_build_landscape(): +def test_build_landscape(pygame_test): landscape = build_landscape() assert isinstance(landscape, Landscape) diff --git a/tests/test_world.py b/tests/test_world.py index 49f03ce..d71eeba 100644 --- a/tests/test_world.py +++ b/tests/test_world.py @@ -10,7 +10,8 @@ @pytest.fixture -def create_world(): +def create_world(mocker): + mocker.patch("doggo.world.pg.init") world = None def _(render_size=None):