Skip to content

Commit 50764b6

Browse files
Refactory test fixtures
1 parent adbfb90 commit 50764b6

13 files changed

Lines changed: 105 additions & 170 deletions

tests/test_monero_common.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,15 @@
55
MoneroError, MoneroRpcError, SerializableStruct
66
)
77

8+
from utils import BaseTestClass
9+
810
logger: logging.Logger = logging.getLogger("TestMoneroCommon")
911

1012

1113
@pytest.mark.unit
12-
class TestMoneroCommon:
14+
class TestMoneroCommon(BaseTestClass):
1315
"""Monero common unit tests"""
1416

15-
@pytest.fixture(autouse=True)
16-
def setup_and_teardown(self, request: pytest.FixtureRequest):
17-
logger.info(f"Before {request.node.name}") # type: ignore
18-
yield
19-
logger.info(f"After {request.node.name}") # type: ignore
20-
2117
# test monero error inheritance
2218
def test_monero_error(self) -> None:
2319
monero_err: MoneroError = MoneroError("Test monero error")

tests/test_monero_connection_manager.py

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import pytest
22
import logging
33

4-
from typing import Optional
4+
from typing import Optional, override
55
from monero import (
66
MoneroConnectionManager, MoneroRpcConnection, MoneroConnectionPollType
77
)
88
from utils import (
99
ConnectionChangeCollector, TestUtils as Utils,
10-
AssertUtils, RpcConnectionUtils
10+
AssertUtils, RpcConnectionUtils, BaseTestClass
1111
)
1212

1313
logger: logging.Logger = logging.getLogger("TestMoneroConnectionManager")
1414

1515

1616
@pytest.mark.integration
17-
class TestMoneroConnectionManager:
17+
class TestMoneroConnectionManager(BaseTestClass):
1818
"""Connection manager integration tests"""
1919

2020
OFFLINE_PROXY_URI: str = "127.0.0.1:9050"
@@ -25,39 +25,22 @@ class TestMoneroConnectionManager:
2525

2626
#region Fixtures
2727

28-
# Setup and teardown of test class
29-
@pytest.fixture(scope="class", autouse=True)
30-
def global_setup_and_teardown(self):
31-
"""Executed once before all tests"""
32-
self.before_all()
33-
yield
34-
self.after_all()
35-
3628
# Before all tests
29+
@override
3730
def before_all(self) -> None:
38-
"""Executed once before all tests"""
39-
logger.info(f"Setup test class {type(self).__name__}")
31+
super().before_all()
4032
self._cm = MoneroConnectionManager()
4133

4234
# After all tests
35+
@override
4336
def after_all(self) -> None:
44-
"""Executed once after all tests"""
45-
logger.info(f"Teardown test class {type(self).__name__}")
37+
super().after_all()
4638
if self._cm:
4739
self._cm.reset()
4840
logger.debug("Resetted connection manager")
49-
else:
50-
logger.warning("Test connection manager is not set!")
5141

5242
Utils.RPC_WALLET_MANAGER.clear()
5343

54-
# setup and teardown of each test
55-
@pytest.fixture(autouse=True)
56-
def setup_and_teardown(self, request: pytest.FixtureRequest):
57-
logger.info(f"Before {request.node.name}") # type: ignore
58-
yield
59-
logger.info(f"After {request.node.name}") # type: ignore
60-
6144
# test connnections fixture
6245
@pytest.fixture(scope="class")
6346
def connections(self) -> list[MoneroRpcConnection]:
@@ -123,7 +106,7 @@ def test_connection_manager(self, connection_manager: MoneroConnectionManager, c
123106

124107
# auto connect to the best available connection
125108
connection_manager.start_polling(Utils.SYNC_PERIOD_IN_MS)
126-
listener.wait_for_change(num_expected_changes + 1, Utils.SYNC_PERIOD_IN_MS, f"Waiting for auto connect to best available connection")
109+
listener.wait_for_change(num_expected_changes + 1, Utils.SYNC_PERIOD_IN_MS, "Waiting for auto connect to best available connection")
127110
assert connection_manager.is_connected()
128111
connection = connection_manager.get_connection()
129112
assert connection is not None

tests/test_monero_daemon_interface.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,16 @@
22
import logging
33

44
from monero import MoneroDaemon, MoneroBan
5+
from utils import BaseTestClass
56

67
logger: logging.Logger = logging.getLogger("TestMoneroDaemonInterface")
78

89

910
# Test calls to MoneroDaemon interface
1011
@pytest.mark.unit
11-
class TestMoneroDaemonInterface:
12+
class TestMoneroDaemonInterface(BaseTestClass):
1213
"""Daemon interface bindings unit tests"""
1314

14-
@pytest.fixture(autouse=True)
15-
def setup_and_teardown(self, request: pytest.FixtureRequest):
16-
logger.info(f"Before {request.node.name}") # type: ignore
17-
yield
18-
logger.info(f"After {request.node.name}") # type: ignore
19-
2015
@pytest.fixture(scope="class")
2116
def daemon(self) -> MoneroDaemon:
2217
"""Test daemon interface instance"""

tests/test_monero_daemon_rpc.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import time
33
import logging
44

5+
from typing import override
6+
57
from monero import (
68
MoneroDaemonRpc, MoneroVersion, MoneroBlockHeader, MoneroBlockTemplate,
79
MoneroBlock, MoneroMiningStatus, MoneroPruneResult,
@@ -20,30 +22,25 @@
2022
BlockUtils, GenUtils,
2123
DaemonUtils, WalletType,
2224
IntegrationTestUtils,
23-
SubmitThenRelayTxTester
25+
SubmitThenRelayTxTester, BaseTestClass
2426
)
2527

2628
logger: logging.Logger = logging.getLogger("TestMoneroDaemonRpc")
2729

2830

2931
@pytest.mark.integration
30-
class TestMoneroDaemonRpc:
32+
class TestMoneroDaemonRpc(BaseTestClass):
3133
"""Rpc daemon integration tests"""
3234
BINARY_BLOCK_CTX: BinaryBlockContext = BinaryBlockContext()
3335
_test_wallet: MoneroWalletRpc | None = None
3436

3537
#region Fixtures
3638

37-
@pytest.fixture(scope="class", autouse=True)
39+
@override
3840
def before_all(self):
41+
# setup wallet rpc for tests
3942
IntegrationTestUtils.setup(WalletType.RPC)
4043

41-
@pytest.fixture(autouse=True)
42-
def setup_and_teardown(self, request: pytest.FixtureRequest):
43-
logger.info(f"Before {request.node.name}") # type: ignore
44-
yield
45-
logger.info(f"After {request.node.name}") # type: ignore
46-
4744
@pytest.fixture(scope="class")
4845
def daemon(self) -> MoneroDaemonRpc:
4946
"""Test rpc daemon instance"""

tests/test_monero_rpc_connection.py

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,20 @@
55
MoneroRpcConnection, MoneroConnectionType, MoneroRpcError,
66
MoneroUtils, MoneroConnectionProriotyComparator
77
)
8-
from utils import TestUtils as Utils, DaemonUtils, StringUtils
8+
from utils import TestUtils as Utils, DaemonUtils, StringUtils, BaseTestClass
99

1010
logger: logging.Logger = logging.getLogger("TestMoneroRpcConnection")
1111

1212

1313
@pytest.mark.integration
14-
class TestMoneroRpcConnection:
14+
class TestMoneroRpcConnection(BaseTestClass):
1515
"""Rpc connection integration tests"""
1616

1717
TIMEOUT_MS: int = Utils.AUTO_CONNECT_TIMEOUT_MS * 5
1818
"""Rpc connection timeout in milliseconds."""
1919

2020
# region Fixtures
2121

22-
# Setup and teardown of test class
23-
@pytest.fixture(scope="class", autouse=True)
24-
def global_setup_and_teardown(self):
25-
"""Executed once before all tests"""
26-
logger.info(f"Setup test class {type(self).__name__}")
27-
self.before_all()
28-
yield
29-
logger.info(f"Teardown test class {type(self).__name__}")
30-
31-
# Before all tests
32-
def before_all(self) -> None:
33-
"""Executed once before all tests"""
34-
logger.info(f"Setup test class {type(self).__name__}")
35-
36-
# Setup and teardown of each tests
37-
@pytest.fixture(autouse=True)
38-
def setup_and_teardown(self, request: pytest.FixtureRequest):
39-
logger.info(f"Before {request.node.name}") # type: ignore
40-
yield
41-
logger.info(f"After {request.node.name}") # type: ignore
42-
4322
# Node rpc connection fixture
4423
@pytest.fixture(scope="class")
4524
def node_connection(self) -> MoneroRpcConnection:

tests/test_monero_utils.py

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
from monero import (
99
MoneroNetworkType, MoneroIntegratedAddress, MoneroUtils, MoneroTxConfig
1010
)
11-
from utils import AddressBook, KeysBook, WalletUtils
11+
from utils import AddressBook, KeysBook, WalletUtils, BaseTestClass
1212

1313
logger: logging.Logger = logging.getLogger("TestMoneroUtils")
1414

1515

1616
@pytest.mark.unit
17-
class TestMoneroUtils:
17+
class TestMoneroUtils(BaseTestClass):
1818
"""Monero utilities unit tests"""
1919

2020
class Config:
@@ -59,22 +59,6 @@ def config(self) -> TestMoneroUtils.Config:
5959
parser.read('tests/config/test_monero_utils.ini')
6060
return TestMoneroUtils.Config.parse(parser)
6161

62-
# Setup and teardown of test class
63-
@pytest.fixture(scope="class", autouse=True)
64-
def global_setup_and_teardown(self):
65-
"""Executed once before all tests"""
66-
logger.info(f"Setup test class {type(self).__name__}")
67-
yield
68-
logger.info(f"Teardown test class {type(self).__name__}")
69-
70-
# Setup and teardown of each tests
71-
@pytest.fixture(autouse=True)
72-
def setup_and_teardown(self, request: pytest.FixtureRequest):
73-
"""Executed once before each test"""
74-
logger.info(f"Before {request.node.name}") # type: ignore
75-
yield
76-
logger.info(f"After {request.node.name}") # type: ignore
77-
7862
#endregion
7963

8064
#region Tests

tests/test_monero_wallet_common.py

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
import pytest
44
import logging
55

6+
from typing import override
67
from time import sleep
78
from random import shuffle
89
from configparser import ConfigParser
9-
from abc import ABC, abstractmethod
10+
from abc import abstractmethod
1011
from typing import Optional
1112
from datetime import datetime
1213

@@ -28,14 +29,14 @@
2829
ViewOnlyAndOfflineWalletTester,
2930
WalletNotificationCollector,
3031
MiningUtils, SendAndUpdateTxsTester,
31-
SyncWithPoolSubmitTester
32+
SyncWithPoolSubmitTester, BaseTestClass
3233
)
3334

3435
logger: logging.Logger = logging.getLogger("TestMoneroWalletCommon")
3536

3637

3738
# Base class for common wallet tests
38-
class BaseTestMoneroWallet(ABC):
39+
class BaseTestMoneroWallet(BaseTestClass):
3940
"""Common wallet tests that every Monero wallet implementation should support"""
4041
CREATED_WALLET_KEYS_ERROR: str = "Wallet created from keys is not connected to authenticated daemon"
4142
_test_wallet: Optional[MoneroWallet] = None
@@ -177,50 +178,27 @@ def wallet(self) -> MoneroWallet:
177178
"""Test wallet instance"""
178179
pytest.skip("No wallet test instance setup")
179180

180-
# Setup and teardown of test class
181-
@pytest.fixture(scope="class", autouse=True)
182-
def global_setup_and_teardown(self):
183-
"""Executed once before all tests"""
184-
self.before_all()
185-
yield
186-
self.after_all()
187-
188-
# Setup and teardown of each test
189-
@pytest.fixture(autouse=True)
190-
def setup_and_teardown(self, request: pytest.FixtureRequest):
191-
self.before_each(request)
192-
yield
193-
self.after_each(request)
194-
195181
# Before all tests
182+
@override
196183
def before_all(self) -> None:
197-
"""Executed once before all tests"""
198-
logger.info(f"Setup test class {type(self).__name__}")
184+
super().before_all()
199185
IntegrationTestUtils.setup(self.get_wallet_type())
200186

201187
# After all tests
188+
@override
202189
def after_all(self) -> None:
203-
"""Executed once after all tests"""
204-
logger.info(f"Teardown test class {type(self).__name__}")
205-
daemon: MoneroDaemonRpc | None = self._get_test_daemon()
206-
try:
207-
daemon.stop_mining()
208-
except Exception as e:
209-
logger.debug(str(e))
190+
super().after_all()
191+
daemon: MoneroDaemonRpc = self._get_test_daemon()
192+
MiningUtils.try_stop_mining(daemon)
210193

211194
# close wallet
212195
wallet = self.get_test_wallet()
213196
wallet.close(True)
214197

215198
# Before each test
199+
@override
216200
def before_each(self, request: pytest.FixtureRequest) -> None:
217-
"""
218-
Executed before each test
219-
220-
:param pytest.FixtureRequest: Request fixture
221-
"""
222-
logger.info(f"Before {request.node.name}") # type: ignore
223-
201+
super().before_each(request)
224202
daemon = self._get_test_daemon()
225203
wallet = self.get_test_wallet()
226204
status = daemon.get_mining_status()
@@ -229,14 +207,9 @@ def before_each(self, request: pytest.FixtureRequest) -> None:
229207
wallet.stop_mining()
230208

231209
# After each test
210+
@override
232211
def after_each(self, request: pytest.FixtureRequest) -> None:
233-
"""
234-
Executed after each test
235-
236-
:param pytest.FixtureRequest: Request fixture
237-
"""
238-
logger.info(f"After {request.node.name}") # type: ignore
239-
212+
super().after_each(request)
240213
daemon = self._get_test_daemon()
241214
status = daemon.get_mining_status()
242215

tests/test_monero_wallet_interface.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,21 @@
88
MoneroTxWallet
99
)
1010

11-
from utils import WalletUtils, StringUtils
11+
from utils import WalletUtils, StringUtils, BaseTestClass
1212

1313
logger: logging.Logger = logging.getLogger("TestMoneroWalletInterface")
1414

1515

1616
# Test binding calls to MoneroWallet interface
1717
@pytest.mark.unit
18-
class TestMoneroWalletInterface:
18+
class TestMoneroWalletInterface(BaseTestClass):
1919
"""Wallet interface binding calls unit tests"""
2020

2121
@pytest.fixture(scope="class")
2222
def wallet(self) -> MoneroWallet:
2323
"""Test wallet instance"""
2424
return MoneroWallet()
2525

26-
# Setup and teardown of test class
27-
@pytest.fixture(scope="class", autouse=True)
28-
def global_setup_and_teardown(self):
29-
"""Executed once before all tests"""
30-
logger.info(f"Setup test class {type(self).__name__}")
31-
yield
32-
logger.info(f"Teardown test class {type(self).__name__}")
33-
34-
# Setup and teardown of each tests
35-
@pytest.fixture(autouse=True)
36-
def setup_and_teardown(self, request: pytest.FixtureRequest):
37-
logger.info(f"Before {request.node.name}") # type: ignore
38-
yield
39-
logger.info(f"After {request.node.name}") # type: ignore
40-
4126
#region Tests
4227

4328
# Test default language static property

0 commit comments

Comments
 (0)