-
Notifications
You must be signed in to change notification settings - Fork 12
Test runner #2
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
Merged
Merged
Test runner #2
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d652032
Add unit tests for Runner and Reporter classes
paulz 96ac724
Update local-development.md to format project structure with code block
paulz 2e5d9fe
Fix mutable default
paulz bad92c2
Add Qodana configuration file for code analysis
paulz 2c49091
fix: black formatting
paulz 1724556
refactor: with sonnet
paulz 223b83f
refactor: rename run_loop to run_multiple for clarity and consistency
paulz 9af1cf8
fix mypy and imports
paulz b727928
test: better examples
paulz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import os | ||
|
|
||
|
|
||
| ROOT_DIR = os.path.dirname(os.path.abspath(os.path.join("..", __file__))) | ||
| ROOT_DIR = os.path.dirname(os.path.abspath(os.path.join("..", __file__))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| #-------------------------------------------------------------------------------# | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tell me more about this tool.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. built in for PyCharm: https://www.jetbrains.com/qodana/ |
||
| # Qodana analysis is configured by qodana.yaml file # | ||
| # https://www.jetbrains.com/help/qodana/qodana-yaml.html # | ||
| #-------------------------------------------------------------------------------# | ||
| version: "1.0" | ||
| #Specify inspection profile for code analysis | ||
| profile: | ||
| name: qodana.starter | ||
| #Enable inspections | ||
| #include: | ||
| # - name: <SomeEnabledInspectionId> | ||
| #Disable inspections | ||
| #exclude: | ||
| # - name: <SomeDisabledInspectionId> | ||
| # paths: | ||
| # - <path/where/not/run/inspection> | ||
| #Execute shell command before Qodana execution (Applied in CI/CD pipeline) | ||
| #bootstrap: sh ./prepare-qodana.sh | ||
| #Install IDE plugins before Qodana execution (Applied in CI/CD pipeline) | ||
| #plugins: | ||
| # - id: <plugin.id> #(plugin id can be found at https://plugins.jetbrains.com) | ||
| #Specify Qodana linter for analysis (Applied in CI/CD pipeline) | ||
| linter: jetbrains/qodana-python:2024.3 | ||
| exclude: | ||
| - name: All | ||
| paths: | ||
| - docs | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,59 @@ | ||
| import os | ||
| from typing import Callable, List, Optional | ||
|
|
||
| from .reporter import Reporter | ||
| from typing import Callable, Any | ||
|
|
||
|
|
||
| class Runner: | ||
| def __init__(self, test_function: Callable[..., Any], reporter: Reporter) -> None: | ||
| """Executes test functions and collects results using a reporter.""" | ||
|
|
||
| def __init__(self, test_function: Callable[[Reporter], bool], reporter: Reporter) -> None: | ||
| """ | ||
| Initialize the Runner with a test function and reporter. | ||
|
|
||
| Args: | ||
| test_function: Function to execute during test runs | ||
| reporter: Reporter instance to track and report test results | ||
| """ | ||
| self.reporter = reporter | ||
| self.test_function = test_function | ||
|
|
||
| @staticmethod | ||
| def sample_size(default_size: int = 1) -> int: | ||
| def get_sample_size(default_size: int = 1) -> int: | ||
| """ | ||
| Get sample size from environment variable or use default. | ||
|
|
||
| Args: | ||
| default_size: Default sample size if not specified in environment | ||
|
|
||
| Returns: | ||
| Number of test runs to perform | ||
| """ | ||
| return int(os.getenv("CAT_AI_SAMPLE_SIZE", str(default_size))) | ||
|
|
||
| def run_once(self, run_number: int = 0) -> Any: | ||
| def run_once(self, run_number: int = 0) -> bool: | ||
| """ | ||
| Execute the test function once. | ||
|
|
||
| Args: | ||
| run_number: Current run index for reporting | ||
|
|
||
| Returns: | ||
| Result from the test function | ||
| """ | ||
| self.reporter.run_number = run_number | ||
| result = self.test_function(reporter=self.reporter) | ||
| return result | ||
|
|
||
| def run_loop(self, tries: int = sample_size()) -> list[Any]: | ||
| results = [] | ||
| for x in range(0, tries): | ||
| results.append(self.run_once(x)) | ||
| return results | ||
| return self.test_function(self.reporter) | ||
|
|
||
| def run_multiple(self, sample_size: Optional[int] = None) -> List[bool]: | ||
| """ | ||
| Execute the test function multiple times based on sample size. | ||
|
|
||
| Args: | ||
| sample_size: Number of times to run the test, defaults to | ||
| value from get_sample_size() if None | ||
|
|
||
| Returns: | ||
| List of results from all test runs | ||
| """ | ||
| runs = sample_size if sample_size is not None else self.get_sample_size() | ||
| return [self.run_once(i) for i in range(runs)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| from src.cat_ai.reporter import Reporter | ||
| from src.cat_ai.runner import Runner | ||
|
|
||
|
|
||
| # Dummy test function that will be passed to Runner | ||
| def dummy_test_function(reporter: Reporter) -> bool: | ||
| # Imagine that this function does something meaningful | ||
| # Simply returning True instead of trying to log | ||
| return True | ||
|
|
||
|
|
||
| def test_runner_sample_size(monkeypatch): | ||
| # Set an environment variable to test | ||
| monkeypatch.setenv("CAT_AI_SAMPLE_SIZE", "5") | ||
| assert Runner.get_sample_size() == 5 | ||
|
|
||
| # Test default size | ||
| monkeypatch.delenv("CAT_AI_SAMPLE_SIZE", raising=False) | ||
| assert Runner.get_sample_size(default_size=3) == 3 | ||
|
|
||
|
|
||
| def test_run_once(): | ||
| # Create a Reporter with necessary arguments | ||
| reporter = Reporter(test_name="test_run_once", output_dir="/tmp") | ||
paulz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # Initialize Runner with dummy test function and Reporter | ||
| runner = Runner(test_function=dummy_test_function, reporter=reporter) | ||
|
|
||
| # Test run_once | ||
| result = runner.run_once() | ||
| assert result is True | ||
| assert reporter.run_number == 0 | ||
|
|
||
|
|
||
| def test_run_multiple(): | ||
| # Create a Reporter with necessary arguments | ||
| reporter = Reporter(test_name="test_run", output_dir="/tmp") | ||
|
|
||
| # Initialize Runner with dummy test function and Reporter | ||
| runner = Runner(test_function=dummy_test_function, reporter=reporter) | ||
|
|
||
| # Test with explicit sample size parameter | ||
| results = runner.run_multiple(sample_size=2) | ||
| assert len(results) == 2 | ||
| assert all(results) | ||
| expected_results = [True, True] | ||
| assert results == expected_results | ||
|
|
||
|
|
||
| def test_run_with_env_variable(monkeypatch): | ||
| # Set the environment variable for a controlled test | ||
| monkeypatch.setenv("CAT_AI_SAMPLE_SIZE", "3") | ||
|
|
||
| # Create a Reporter with necessary arguments | ||
| reporter = Reporter(test_name="test_run_with_env", output_dir="/tmp") | ||
|
|
||
| # Initialize Runner with dummy test function and Reporter | ||
| runner = Runner(test_function=dummy_test_function, reporter=reporter) | ||
|
|
||
| # Test without explicit sample size (should use environment variable) | ||
| results = runner.run_multiple() | ||
| assert len(results) == 3 | ||
| expected_results = [True, True, True] | ||
| assert results == expected_results | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this is just what your formatter does but which one do you actually think is easier to read?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
may be 120 lines is too big? at 100 formatter would be multiline. what you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does that mean you prefer the multi-line version? I'm open to either.