diff --git a/src/ape/pytest/plugin.py b/src/ape/pytest/plugin.py index 85d5febd5d..7cfe0c106f 100644 --- a/src/ape/pytest/plugin.py +++ b/src/ape/pytest/plugin.py @@ -16,15 +16,6 @@ def _get_default_network(ecosystem: Optional[EcosystemAPI] = None) -> str: if ecosystem is None: ecosystem = ManagerAccessMixin.network_manager.default_ecosystem - if ecosystem.default_network.is_mainnet: - # Don't use mainnet for tests, even if it configured as - # the default. - raise ConfigError( - "Default network is mainnet; unable to run tests on mainnet. " - "Please specify the network using the `--network` flag or " - "configure a different default network." - ) - return ecosystem.name diff --git a/src/ape/pytest/runners.py b/src/ape/pytest/runners.py index 0445e8f446..98a708e99b 100644 --- a/src/ape/pytest/runners.py +++ b/src/ape/pytest/runners.py @@ -7,6 +7,7 @@ from rich import print as rich_print from ape.api.networks import ProviderContextManager +from ape.exceptions import ConfigError from ape.logging import LogLevel from ape.pytest.config import ConfigWrapper from ape.pytest.coverage import CoverageTracker @@ -206,8 +207,24 @@ def pytest_collection_finish(self, session): # Only start provider if collected tests. if not outcome.get_result() and session.items: - self._provider_context.push_provider() - self._provider_is_connected = True + self._connect() + + def _connect(self): + if self._provider_context._provider.network.is_mainnet: + # Ensure is not only running on tests on mainnet because + # was configured as the default. + is_from_command_line = ( + "--network" in self.config_wrapper.pytest_config.invocation_params.args + ) + if not is_from_command_line: + raise ConfigError( + "Default network is mainnet; unable to run tests on mainnet. " + "Please specify the network using the `--network` flag or " + "configure a different default network." + ) + + self._provider_context.push_provider() + self._provider_is_connected = True def pytest_terminal_summary(self, terminalreporter): """ diff --git a/tests/functional/test_test.py b/tests/functional/test_test.py index 9666170c56..56ee09c88a 100644 --- a/tests/functional/test_test.py +++ b/tests/functional/test_test.py @@ -1,7 +1,7 @@ import pytest from ape.exceptions import ConfigError -from ape.pytest.plugin import _get_default_network +from ape.pytest.runners import PytestApeRunner from ape_test import ApeTestConfig @@ -15,17 +15,21 @@ def test_balance_set_from_currency_str(self): assert actual == expected -def test_get_default_network(mocker): - # NOTE: Using this weird test to avoid actually - # using mainnet in any test, even accidentally. - mock_ecosystem = mocker.MagicMock() - mock_mainnet = mocker.MagicMock() - mock_mainnet.name = "mainnet" - mock_ecosystem.default_network = mock_mainnet +def test_connect_to_mainnet_by_default(mocker): + """ + Tests the condition where mainnet is configured as the default network + and no --network option is passed. It should avoid running the tests + to be safe. + """ + + cfg = mocker.MagicMock() + cfg.network = "ethereum:mainnet:node" + runner = PytestApeRunner(cfg, mocker.MagicMock(), mocker.MagicMock(), mocker.MagicMock()) + expected = ( "Default network is mainnet; unable to run tests on mainnet. " "Please specify the network using the `--network` flag or " "configure a different default network." ) with pytest.raises(ConfigError, match=expected): - _get_default_network(mock_mainnet) + runner._connect()