Skip to content
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

test_examples.py: Add batch_run tests #144

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions test_examples.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import importlib
import os
import sys

import pytest
from mesa import Model
Expand Down Expand Up @@ -32,3 +33,24 @@ def test_model_steps(model_class):
model = model_class() # Assume no arguments are needed
for _ in range(10):
model.step()


def get_batch_scripts():
return [
("examples.bank_reserves", "batch_run"),
("examples.sugarscape_g1mt", "run"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will take too long given that it will be 1000 steps.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can include some shorter examples somewhere?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, by calling them batch_run_short, run_short.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use a command line argument or an environment variable?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's easier to just name the functions.

]


@pytest.mark.parametrize("example_dir, script_module", get_batch_scripts())
def test_batch_run(example_dir, script_module):
# Save the old sys.path
old_sys_path = sys.path[:]
try:
# Add the example directory to the sys.path
sys.path.insert(0, os.path.abspath(example_dir))
module = importlib.import_module(f"{example_dir}.{script_module}")
module.main() # Call the main function
finally:
# Restore the original sys.path
sys.path = old_sys_path
Loading