From 7eef36c2df9034cfc83131c2cd7dd83094dca79b Mon Sep 17 00:00:00 2001 From: aeon0 Date: Tue, 28 Dec 2021 14:40:28 +0100 Subject: [PATCH] Add Tests and Coverage report (#341) --- .coveragerc | 16 ++++++++++++++++ .github/workflows/ci.yml | 10 ++++++++-- .gitignore | 3 +++ codecov.yml | 1 + environment.yml | 1 + src/game_stats.py | 5 ++--- test/game_stats_test.py | 25 +++++++++++++++++++++++++ 7 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 .coveragerc create mode 100644 codecov.yml create mode 100644 test/game_stats_test.py diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 0000000..367327b --- /dev/null +++ b/.coveragerc @@ -0,0 +1,16 @@ +# .coveragerc to control coverage.py +[run] +branch = True + +[report] +# Regexes for lines to exclude from consideration +exclude_lines = + # Have to re-enable the standard pragma + pragma: no cover + # Don't complain if tests don't hit defensive assertion code: + raise AssertionError + raise NotImplementedError + + # Don't complain if non-runnable code isn't run: + if 0: + if __name__ == "__main__": diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a285bea..6be2e47 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -39,9 +39,15 @@ jobs: run: | C:\Miniconda\condabin\conda.bat init powershell set PYTHONPATH=./src - - name: Pytest + - name: Pytest & Coverage shell: powershell run: | C:\Miniconda\condabin\conda.bat activate botty python -c "import sys; print(sys.version)" - pytest -v -s + coverage run --source=./src -m pytest -v -s + coverage xml + - name: Upload Coverage to Codecov + uses: codecov/codecov-action@v2 + with: + verbose: true + files: ./coverage.xml diff --git a/.gitignore b/.gitignore index 5f5adc1..16d21ee 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,6 @@ info_screenshots/ loot_screenshots/ stats/ .venv +.coverage +htmlcov/ +coverage.xml diff --git a/codecov.yml b/codecov.yml new file mode 100644 index 0000000..69cb760 --- /dev/null +++ b/codecov.yml @@ -0,0 +1 @@ +comment: false diff --git a/environment.yml b/environment.yml index 8fcff11..7d6447a 100644 --- a/environment.yml +++ b/environment.yml @@ -13,6 +13,7 @@ dependencies: - beautifultable - pytweening - requests + - coverage - pytest - pytest-env - pytest-pythonpath diff --git a/src/game_stats.py b/src/game_stats.py index 3547e8d..f2ec9e2 100644 --- a/src/game_stats.py +++ b/src/game_stats.py @@ -14,7 +14,6 @@ class GameStats: def __init__(self): self._config = Config() self._messenger = Messenger() - self._picked_up_items = [] self._start_time = time.time() self._timer = None self._timepaused = None @@ -54,8 +53,8 @@ class GameStats: self._location_stats[self._location] = { "items": [], "deaths": 0, "chickens": 0, "merc_deaths": 0, "failed_runs": 0 } def log_item_pickup(self, item_name: str, send_message: bool): - self._picked_up_items.append(item_name) - if self._location is not None: + filtered_items = ["_potion", "misc_gold"] + if self._location is not None and not any(substring in item_name for substring in filtered_items): self._location_stats[self._location]["items"].append(item_name) if send_message: diff --git a/test/game_stats_test.py b/test/game_stats_test.py new file mode 100644 index 0000000..4e3217d --- /dev/null +++ b/test/game_stats_test.py @@ -0,0 +1,25 @@ +import pytest +from logger import Logger +from game_stats import GameStats + + +class TestGameStats: + def setup_method(self): + Logger.init() + Logger.remove_file_logger() + self.game_stats = GameStats() + + @pytest.mark.parametrize("item_name, item_should_be_added", [ + ("some_magic_item", True), + ("misc_jewel", True), + ("misc_gold", False), + ("some_potion", False), + ("super_healing_potion", False), + ]) + def test_adjust_abs_range_to_screen(self, item_name: str, item_should_be_added: bool): + self.game_stats.update_location("test_location") + previous_item_count = len(self.game_stats._location_stats["test_location"]["items"]) + self.game_stats.log_item_pickup(item_name, send_message=False) + new_item_count = len(self.game_stats._location_stats["test_location"]["items"]) + item_was_added = previous_item_count < new_item_count + assert(item_was_added == item_should_be_added)