From c4d9b9264a3f814a9185b6e22d31343c30040389 Mon Sep 17 00:00:00 2001 From: PulpCattel Date: Sat, 14 Jan 2023 19:14:48 +0000 Subject: [PATCH] tests: make setup fixtures optional. This allows running tests that do not require, e.g., bitcoind, without having to run these setup fixtures at all. Signed-off-by: PulpCattel --- conftest.py | 25 ++++++++++++++++++++++- jmclient/test/test_blockchaininterface.py | 2 ++ jmclient/test/test_bond_calc.py | 2 ++ jmclient/test/test_client_protocol.py | 5 +++++ jmclient/test/test_coinjoin.py | 2 ++ jmclient/test/test_commitment_utils.py | 3 +++ jmclient/test/test_configure.py | 2 ++ jmclient/test/test_core_nohistory_sync.py | 2 ++ jmclient/test/test_payjoin.py | 4 ++++ jmclient/test/test_podle.py | 3 +++ jmclient/test/test_privkeys.py | 3 +++ jmclient/test/test_psbt_wallet.py | 3 +++ jmclient/test/test_schedule.py | 2 ++ jmclient/test/test_snicker.py | 2 ++ jmclient/test/test_taker.py | 3 +++ jmclient/test/test_tx_creation.py | 2 ++ jmclient/test/test_valid_addresses.py | 3 +++ jmclient/test/test_wallet.py | 1 + jmclient/test/test_wallet_rpc.py | 4 ++++ jmclient/test/test_wallets.py | 3 +++ jmclient/test/test_walletservice.py | 3 +++ jmclient/test/test_walletutils.py | 3 +++ jmclient/test/test_yieldgenerator.py | 4 ++++ jmdaemon/test/test_daemon_protocol.py | 5 +++++ jmdaemon/test/test_irc_messaging.py | 4 ++++ jmdaemon/test/test_orderbookwatch.py | 2 ++ test/test_e2e_coinjoin.py | 2 ++ 27 files changed, 98 insertions(+), 1 deletion(-) diff --git a/conftest.py b/conftest.py index 1ded3ad..3d0ea25 100644 --- a/conftest.py +++ b/conftest.py @@ -82,6 +82,29 @@ def pytest_addoption(parser: Any) -> None: @pytest.fixture(scope="session", autouse=True) +def setup_early_if_needed(request) -> None: + """ + Make sure fixtures requested by test *modules* are executed first. + I.e., like a dynamically set `autouse=True`. + (By default, without `autouse=True`, they would run later at request time) + Useful so that fixtures like `setup_regtest_bitcoind` can run *only* if + we are planning to invoke a test that requires it, but still at startup time. + """ + modules = set() + # Loop through the collected tests + for item in request.node.items: + module = item.getparent(pytest.Module) + if module in modules: + continue + modules.add(module) + # Loop through each test module marker + for marker in module.iter_markers('usefixtures'): + # We know we are gonna need these fixtures, so we invoke them early + for fixture in marker.args: + request.getfixturevalue(fixture) + + +@pytest.fixture(scope="session") def setup_miniircd(pytestconfig): """ Setup miniircd and handle its clean up. @@ -102,7 +125,7 @@ def setup_miniircd(pytestconfig): m.kill() -@pytest.fixture(scope="session", autouse=True) +@pytest.fixture(scope="session") def setup_regtest_bitcoind(pytestconfig): """ Setup regtest bitcoind and handle its clean up. diff --git a/jmclient/test/test_blockchaininterface.py b/jmclient/test/test_blockchaininterface.py index 435b631..267066b 100644 --- a/jmclient/test/test_blockchaininterface.py +++ b/jmclient/test/test_blockchaininterface.py @@ -10,6 +10,8 @@ from jmclient import load_test_config, jm_single, BaseWallet log = get_log() +pytestmark = pytest.mark.usefixtures("setup_regtest_bitcoind") + def sync_test_wallet(fast, wallet_service): sync_count = 0 diff --git a/jmclient/test/test_bond_calc.py b/jmclient/test/test_bond_calc.py index 8417d88..df3bd90 100644 --- a/jmclient/test/test_bond_calc.py +++ b/jmclient/test/test_bond_calc.py @@ -4,6 +4,8 @@ import pytest from jmclient import jm_single, load_test_config, FidelityBondMixin from jmclient.bond_calc import get_next_locktime, get_bond_values, get_percentiles +pytestmark = pytest.mark.usefixtures("setup_regtest_bitcoind") + @pytest.mark.parametrize(('date', 'next_locktime'), ((datetime(2022, 1, 1, 1, 1), datetime(2022, 2, 1)), diff --git a/jmclient/test/test_client_protocol.py b/jmclient/test/test_client_protocol.py index d1f07f9..71dc84c 100644 --- a/jmclient/test/test_client_protocol.py +++ b/jmclient/test/test_client_protocol.py @@ -21,6 +21,11 @@ import json import jmbitcoin as bitcoin import twisted import base64 + +import pytest + +pytestmark = pytest.mark.usefixtures("setup_regtest_bitcoind") + twisted.internet.base.DelayedCall.debug = True test_completed = False diff --git a/jmclient/test/test_coinjoin.py b/jmclient/test/test_coinjoin.py index 516c68b..7d3754b 100644 --- a/jmclient/test/test_coinjoin.py +++ b/jmclient/test/test_coinjoin.py @@ -18,6 +18,8 @@ from commontest import make_wallets, default_max_cj_fee from test_taker import dummy_filter_orderbook import jmbitcoin as btc +pytestmark = pytest.mark.usefixtures("setup_regtest_bitcoind") + testdir = os.path.dirname(os.path.realpath(__file__)) log = get_log() diff --git a/jmclient/test/test_commitment_utils.py b/jmclient/test/test_commitment_utils.py index dc39bd1..fa15f80 100644 --- a/jmclient/test/test_commitment_utils.py +++ b/jmclient/test/test_commitment_utils.py @@ -6,6 +6,9 @@ from jmclient import (load_test_config, jm_single, BTC_P2WPKH) from jmclient.commitment_utils import get_utxo_info, validate_utxo_data from jmbitcoin import select_chain_params +pytestmark = pytest.mark.usefixtures("setup_regtest_bitcoind") + + def test_get_utxo_info(): load_test_config() # this test tests mainnet keys, so temporarily switch network diff --git a/jmclient/test/test_configure.py b/jmclient/test/test_configure.py index 1b34ecd..22b8af5 100644 --- a/jmclient/test/test_configure.py +++ b/jmclient/test/test_configure.py @@ -4,6 +4,8 @@ import pytest from jmclient import load_test_config, jm_single from jmclient.configure import get_blockchain_interface_instance +pytestmark = pytest.mark.usefixtures("setup_regtest_bitcoind") + def test_attribute_dict(): from jmclient.configure import AttributeDict diff --git a/jmclient/test/test_core_nohistory_sync.py b/jmclient/test/test_core_nohistory_sync.py index fe1779c..8717913 100644 --- a/jmclient/test/test_core_nohistory_sync.py +++ b/jmclient/test/test_core_nohistory_sync.py @@ -11,6 +11,8 @@ from jmclient import (load_test_config, SegwitLegacyWallet, SegwitWallet, jm_single, BaseWallet) from jmbitcoin import select_chain_params +pytestmark = pytest.mark.usefixtures("setup_regtest_bitcoind") + log = get_log() def test_fast_sync_unavailable(setup_sync): diff --git a/jmclient/test/test_payjoin.py b/jmclient/test/test_payjoin.py index dabad63..6c3a18d 100644 --- a/jmclient/test/test_payjoin.py +++ b/jmclient/test/test_payjoin.py @@ -4,6 +4,8 @@ Test doing payjoins over tcp client/server """ import os + +import pytest from twisted.internet import reactor from twisted.web.server import Site from twisted.web.client import readBody @@ -26,6 +28,8 @@ from jmclient.payjoin import process_payjoin_proposal_from_server from commontest import make_wallets from test_coinjoin import make_wallets_to_list, sync_wallets +pytestmark = pytest.mark.usefixtures("setup_regtest_bitcoind") + testdir = os.path.dirname(os.path.realpath(__file__)) log = get_log() diff --git a/jmclient/test/test_podle.py b/jmclient/test/test_podle.py index d268f76..a7bb2c9 100644 --- a/jmclient/test/test_podle.py +++ b/jmclient/test/test_podle.py @@ -12,6 +12,9 @@ from jmclient import load_test_config, jm_single, generate_podle,\ get_podle_commitments, add_external_commitments, update_commitments from jmclient.podle import verify_all_NUMS, verify_podle, PoDLEError from commontest import make_wallets + +pytestmark = pytest.mark.usefixtures("setup_regtest_bitcoind") + log = get_log() def test_commitments_empty(setup_podle): diff --git a/jmclient/test/test_privkeys.py b/jmclient/test/test_privkeys.py index 8c18c6b..468c9f1 100644 --- a/jmclient/test/test_privkeys.py +++ b/jmclient/test/test_privkeys.py @@ -7,6 +7,9 @@ from jmclient import BTCEngine, jm_single, load_test_config import json import pytest import os + +pytestmark = pytest.mark.usefixtures("setup_regtest_bitcoind") + testdir = os.path.dirname(os.path.realpath(__file__)) def test_read_raw_privkeys(setup_keys): diff --git a/jmclient/test/test_psbt_wallet.py b/jmclient/test/test_psbt_wallet.py index 8f471c2..1a70466 100644 --- a/jmclient/test/test_psbt_wallet.py +++ b/jmclient/test/test_psbt_wallet.py @@ -17,6 +17,9 @@ from jmclient import (load_test_config, jm_single, direct_send, SegwitLegacyWallet, SegwitWallet, LegacyWallet, VolatileStorage, get_network) from jmclient.wallet import PSBTWalletMixin + +pytestmark = pytest.mark.usefixtures("setup_regtest_bitcoind") + log = get_log() def create_volatile_wallet(seedphrase, wallet_cls=SegwitWallet): diff --git a/jmclient/test/test_schedule.py b/jmclient/test/test_schedule.py index 8b873eb..eb93075 100644 --- a/jmclient/test/test_schedule.py +++ b/jmclient/test/test_schedule.py @@ -6,6 +6,8 @@ from jmclient import (get_schedule, get_tumble_schedule, tweak_tumble_schedule, load_test_config) import os +pytestmark = pytest.mark.usefixtures("setup_regtest_bitcoind") + valids = """#sample for testing 1, 110000000, 3, INTERNAL, 0, 16, 1 0, 20000000, 2, mnsquzxrHXpFsZeL42qwbKdCP2y1esN3qw, 9.88, 16, 0 diff --git a/jmclient/test/test_snicker.py b/jmclient/test/test_snicker.py index 31f4da1..4274a4b 100644 --- a/jmclient/test/test_snicker.py +++ b/jmclient/test/test_snicker.py @@ -10,6 +10,8 @@ from jmbase import get_log, bintohex from jmclient import (load_test_config, estimate_tx_fee, SNICKERReceiver, direct_send, BaseWallet) +pytestmark = pytest.mark.usefixtures("setup_regtest_bitcoind") + log = get_log() @pytest.mark.parametrize( diff --git a/jmclient/test/test_taker.py b/jmclient/test/test_taker.py index 47db342..32714c6 100644 --- a/jmclient/test/test_taker.py +++ b/jmclient/test/test_taker.py @@ -19,6 +19,9 @@ from taker_test_data import t_utxos_by_mixdepth, t_orderbook,\ t_maker_response, t_chosen_orders, t_dummy_ext from commontest import default_max_cj_fee +pytestmark = pytest.mark.usefixtures("setup_regtest_bitcoind") + + def convert_utxos(utxodict): return_dict = {} for uk, val in utxodict.items(): diff --git a/jmclient/test/test_tx_creation.py b/jmclient/test/test_tx_creation.py index 4dec62f..10d297f 100644 --- a/jmclient/test/test_tx_creation.py +++ b/jmclient/test/test_tx_creation.py @@ -13,6 +13,8 @@ import pytest from jmbase import get_log from jmclient import load_test_config, jm_single, direct_send, estimate_tx_fee, compute_tx_locktime +pytestmark = pytest.mark.usefixtures("setup_regtest_bitcoind") + log = get_log() #just a random selection of pubkeys for receiving multisigs; #if we ever need the privkeys, they are in a json file somewhere diff --git a/jmclient/test/test_valid_addresses.py b/jmclient/test/test_valid_addresses.py index 2ebdf43..cacebe0 100644 --- a/jmclient/test/test_valid_addresses.py +++ b/jmclient/test/test_valid_addresses.py @@ -7,6 +7,9 @@ from bitcointx import ChainParams import json import pytest import os + +pytestmark = pytest.mark.usefixtures("setup_regtest_bitcoind") + testdir = os.path.dirname(os.path.realpath(__file__)) def address_valid_somewhere(addr): diff --git a/jmclient/test/test_wallet.py b/jmclient/test/test_wallet.py index a662807..86d5d8e 100644 --- a/jmclient/test/test_wallet.py +++ b/jmclient/test/test_wallet.py @@ -19,6 +19,7 @@ from test_blockchaininterface import sync_test_wallet from freezegun import freeze_time from bitcointx.wallet import CCoinAddressError +pytestmark = pytest.mark.usefixtures("setup_regtest_bitcoind") testdir = os.path.dirname(os.path.realpath(__file__)) diff --git a/jmclient/test/test_wallet_rpc.py b/jmclient/test/test_wallet_rpc.py index e37116c..cd265b5 100644 --- a/jmclient/test/test_wallet_rpc.py +++ b/jmclient/test/test_wallet_rpc.py @@ -1,4 +1,6 @@ import os, json + +import pytest from twisted.internet import reactor, defer, task from twisted.web.client import readBody, Headers @@ -19,6 +21,8 @@ from test_coinjoin import make_wallets_to_list, sync_wallets from test_websocket import (ClientTProtocol, test_tx_hex_1, test_tx_hex_txid, encoded_token) +pytestmark = pytest.mark.usefixtures("setup_regtest_bitcoind") + testdir = os.path.dirname(os.path.realpath(__file__)) testfilename = "testwrpc" diff --git a/jmclient/test/test_wallets.py b/jmclient/test/test_wallets.py index f085469..675c975 100644 --- a/jmclient/test/test_wallets.py +++ b/jmclient/test/test_wallets.py @@ -13,6 +13,9 @@ from jmclient import ( load_test_config, jm_single, estimate_tx_fee, BitcoinCoreInterface, Mnemonic) from taker_test_data import t_raw_signed_tx + +pytestmark = pytest.mark.usefixtures("setup_regtest_bitcoind") + testdir = os.path.dirname(os.path.realpath(__file__)) log = get_log() diff --git a/jmclient/test/test_walletservice.py b/jmclient/test/test_walletservice.py index f46238b..ec51970 100644 --- a/jmclient/test/test_walletservice.py +++ b/jmclient/test/test_walletservice.py @@ -7,6 +7,9 @@ from jmclient import load_test_config, jm_single, \ WalletService from test_blockchaininterface import sync_test_wallet from test_wallet import fund_wallet_addr, get_populated_wallet + +pytestmark = pytest.mark.usefixtures("setup_regtest_bitcoind") + testdir = os.path.dirname(os.path.realpath(__file__)) log = get_log() diff --git a/jmclient/test/test_walletutils.py b/jmclient/test/test_walletutils.py index cf9ac3a..7454a02 100644 --- a/jmclient/test/test_walletutils.py +++ b/jmclient/test/test_walletutils.py @@ -6,6 +6,9 @@ from jmclient.wallet_utils import (bip32pathparse, WalletView, WalletViewAccount, WalletViewBranch, WalletViewEntry, wallet_signmessage) +pytestmark = pytest.mark.usefixtures("setup_regtest_bitcoind") + + # The below signatures have all been verified against Electrum 4.0.9: @pytest.mark.parametrize('seed, hdpath, walletcls, message, sig, addr', [ [b"\x01"*16, "m/84'/0'/0'/0/0", SegwitWallet, "hello", diff --git a/jmclient/test/test_yieldgenerator.py b/jmclient/test/test_yieldgenerator.py index 27611a1..54d3f9c 100644 --- a/jmclient/test/test_yieldgenerator.py +++ b/jmclient/test/test_yieldgenerator.py @@ -1,10 +1,14 @@ import unittest + +import pytest from jmbitcoin import CMutableTxOut, CMutableTransaction from jmclient import load_test_config, jm_single,\ SegwitLegacyWallet, VolatileStorage, YieldGeneratorBasic, \ get_network, WalletService +pytestmark = pytest.mark.usefixtures("setup_regtest_bitcoind") + class CustomUtxoWallet(SegwitLegacyWallet): """A wallet instance that makes it easy to do the tasks we need for diff --git a/jmdaemon/test/test_daemon_protocol.py b/jmdaemon/test/test_daemon_protocol.py index f8e9a6d..925b624 100644 --- a/jmdaemon/test/test_daemon_protocol.py +++ b/jmdaemon/test/test_daemon_protocol.py @@ -22,6 +22,11 @@ from msgdata import * import base64 import sys from dummy_mc import DummyMessageChannel + +import pytest + +pytestmark = pytest.mark.usefixtures("setup_regtest_bitcoind") + test_completed = False end_early = False jlog = get_log() diff --git a/jmdaemon/test/test_irc_messaging.py b/jmdaemon/test/test_irc_messaging.py index 0e9812f..8c502ed 100644 --- a/jmdaemon/test/test_irc_messaging.py +++ b/jmdaemon/test/test_irc_messaging.py @@ -8,6 +8,10 @@ from jmdaemon import IRCMessageChannel, MessageChannelCollection #needed for test framework from jmclient import (load_test_config, get_mchannels, jm_single) +import pytest + +pytestmark = pytest.mark.usefixtures("setup_miniircd", "setup_regtest_bitcoind") + si = 1 class DummyDaemon(object): def request_signature_verify(self, a, b, c, d, e, diff --git a/jmdaemon/test/test_orderbookwatch.py b/jmdaemon/test/test_orderbookwatch.py index 17797a6..97e30f2 100644 --- a/jmdaemon/test/test_orderbookwatch.py +++ b/jmdaemon/test/test_orderbookwatch.py @@ -7,6 +7,8 @@ from jmdaemon.protocol import JM_VERSION, ORDER_KEYS from jmbase.support import hextobin from jmclient.fidelity_bond import FidelityBondProof +pytestmark = pytest.mark.usefixtures("setup_regtest_bitcoind") + class DummyDaemon(object): def request_signature_verify(self, a, b, c, d, e, f, g, h): diff --git a/test/test_e2e_coinjoin.py b/test/test_e2e_coinjoin.py index df0d609..109db88 100644 --- a/test/test_e2e_coinjoin.py +++ b/test/test_e2e_coinjoin.py @@ -27,6 +27,8 @@ from jmclient import (YieldGeneratorBasic, load_test_config, jm_single, import jmclient from jmclient.wallet_rpc import api_version_string +pytestmark = pytest.mark.usefixtures("setup_regtest_bitcoind") + log = get_log() wallet_name = "test-onion-yg-runner.jmdat"