From f81b08b0202958f920005aabe49fc751245b7f3c Mon Sep 17 00:00:00 2001 From: Ben Hauser Date: Thu, 22 Oct 2020 19:24:30 +0400 Subject: [PATCH] refactor: connect to network immediatly prior to running tests --- brownie/test/managers/runner.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/brownie/test/managers/runner.py b/brownie/test/managers/runner.py index 83bd80ca4..690d62eb8 100644 --- a/brownie/test/managers/runner.py +++ b/brownie/test/managers/runner.py @@ -195,11 +195,6 @@ def pytest_collection_modifyitems(self, items): tests[path][0].parent.add_marker("skip") self.isolated[path] = set(self.tests[path]["isolated"]) - # only connect to network if there are tests to run - to_run = any(i for i in items if not i.get_closest_marker("skip")) - if to_run and not self.config.getoption("--fixtures"): - brownie.network.connect(CONFIG.argv["network"]) - def _check_updated(self, path): path = self._path(path) if path not in self.tests or not self.tests[path]["isolated"]: @@ -216,6 +211,26 @@ def _make_nodemap(self, ids): path, test = self._test_id(item) self.node_map.setdefault(path, []).append(test) + @pytest.hookimpl(trylast=True, hookwrapper=True) + def pytest_collection_finish(self, session): + """ + Called after collection has been performed and modified. + + This is the final hookpoint that executes prior to running tests. If + the number of tests collected is > 0 and there is not an active network + at this point, Brownie connects to the the default network and launches + the RPC client if required. + + Arguments + --------- + session : pytest.Session + The pytest session object. + """ + outcome = yield + # handled as a hookwrapper to ensure connecting is the last action for this hook + if not outcome.get_result() and session.items and not brownie.network.is_connected(): + brownie.network.connect(CONFIG.argv["network"]) + def pytest_runtest_protocol(self, item): """ Implements the runtest_setup/call/teardown protocol for the given test item,