Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Commit d97e955

Browse files
committed
Cleanup test loading
1 parent 80c5cdf commit d97e955

12 files changed

+153
-146
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ sudo: false
44
env:
55
- TOX_ENV=py27
66
install:
7-
- pip install -r requirements.txt
8-
- pip install -r dev_requirements.txt
7+
- pip install -Ur requirements.txt
8+
- pip install -Ur dev_requirements.txt
99
script:
1010
- coverage run --source ethereum -m py.test --ignore ethereum/tests/test_vm.py --ignore
1111
ethereum/tests/test_state.py

dev_requirements.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
tox
22
coveralls
3-
pytest
4-
pytest-catchlog==1.1
5-
pytest-timeout==0.5
3+
pytest>=2.9.0
4+
pytest-catchlog==1.2.2
5+
pytest-timeout==1.0.0
66
https://github.com/ethereum/serpent/tarball/develop

ethereum/tests/conftest.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
import pytest
2+
23
from ethereum import slogging
34

45

56
CATCH_LOG_HANDLER_NAME = 'catch_log_handler'
67

78

8-
@pytest.mark.hookwrapper
9-
def pytest_runtest_setup(item):
10-
"""Attach pytest-catchlog's handler to `slogging`'s root logger"""
11-
yield
12-
caplog_handler = getattr(item, CATCH_LOG_HANDLER_NAME, None)
13-
if caplog_handler and caplog_handler not in slogging.rootLogger.handlers:
14-
slogging.rootLogger.addHandler(caplog_handler)
9+
# Connect catchlog's handler to slogging's root logger
10+
@pytest.hookimpl(hookwrapper=True)
11+
def pytest_runtest_call(item):
12+
catchlog_handler = getattr(item, CATCH_LOG_HANDLER_NAME, None)
13+
if catchlog_handler and catchlog_handler not in slogging.rootLogger.handlers:
14+
slogging.rootLogger.addHandler(catchlog_handler)
1515

16+
_ = yield
1617

17-
@pytest.mark.hookwrapper
18-
def pytest_runtest_makereport(item, call):
19-
"""Remove pytest-catchlog's handler from `slogging`'s root logger"""
20-
if call.when == 'call':
21-
caplog_handler = getattr(item, CATCH_LOG_HANDLER_NAME, None)
22-
if caplog_handler and caplog_handler in slogging.rootLogger.handlers:
23-
slogging.rootLogger.removeHandler(caplog_handler)
24-
yield
18+
if catchlog_handler and catchlog_handler in slogging.rootLogger.handlers:
19+
slogging.rootLogger.removeHandler(catchlog_handler)

ethereum/tests/test_abi.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,24 @@
44
import ethereum.abi as abi
55
logger = get_logger()
66

7+
78
def test_abi_encode_var_sized_array():
89
abi.encode_abi(['address[]'], [[b'\x00' * 20] * 3])
910

11+
1012
def test_abi_encode_fixed_size_array():
1113
abi.encode_abi(['uint16[2]'], [[5, 6]])
1214

15+
1316
def test_abi_encode_signed_int():
1417
assert abi.decode_abi(['int8'], abi.encode_abi(['int8'], [1]))[0] == 1
1518
assert abi.decode_abi(['int8'], abi.encode_abi(['int8'], [-1]))[0] == -1
1619

1720

18-
# SETUP TESTS IN GLOBAL NAME SPACE
19-
def gen_func(filename, testname, testdata):
20-
return lambda: do_test_state(filename, testname, testdata)
21-
22-
def do_test_state(filename, testname=None, testdata=None, limit=99999999):
23-
logger.debug('running test:%r in %r' % (testname, filename))
21+
# Will be parametrized fron json fixtures
22+
def test_state(filename, testname, testdata):
2423
testutils.check_abi_test(testutils.fixture_to_bytes(testdata))
2524

26-
fixtures = testutils.get_tests_from_file_or_dir(
27-
os.path.join(testutils.fixture_path, 'ABITests'))
2825

29-
filenames = sorted(list(fixtures.keys()))
30-
for filename in filenames:
31-
tests = fixtures[filename]
32-
for testname, testdata in list(tests.items()):
33-
func_name = 'test_%s_%s' % (filename, testname)
34-
globals()[func_name] = gen_func(filename, testname, testdata)
26+
def pytest_generate_tests(metafunc):
27+
testutils.generate_test_params('ABITests', metafunc)

ethereum/tests/test_blocks.py

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import pytest
2+
13
from ethereum import blocks, utils, db
24
from ethereum.exceptions import VerificationFailed, InvalidTransaction
35
import rlp
@@ -106,41 +108,44 @@ def run_block_test(params, config_overrides = {}):
106108
env.config = old_config
107109

108110

109-
def do_test_block(filename, testname=None, testdata=None, limit=99999999):
110-
print('\nrunning test:%r in %r' % (testname, filename))
111+
def test_block(filename, testname, testdata):
111112
run_block_test(testdata, {'HOMESTEAD_FORK_BLKNUM':0 if 'Homestead' in filename else 5 if 'TestNetwork' in filename else 1000000 })
112113

113-
excludes = [('bcWalletTest.json', u'walletReorganizeOwners'),
114-
('bl10251623GO.json', u'randomBlockTest'),
115-
('bl201507071825GO.json', u'randomBlockTest')
116-
]
117114

115+
excludes = {
116+
('bcWalletTest.json', u'walletReorganizeOwners'),
117+
('bl10251623GO.json', u'randomBlockTest'),
118+
('bl201507071825GO.json', u'randomBlockTest')
119+
}
118120

119-
if __name__ == '__main__':
121+
122+
def pytest_generate_tests(metafunc):
123+
testutils.generate_test_params(
124+
'BlockchainTests',
125+
metafunc,
126+
lambda filename, testname, _: (filename.split('/')[-1], testname) in excludes
127+
)
128+
129+
130+
def main():
120131
assert len(sys.argv) >= 2, "Please specify file or dir name"
121132
fixtures = testutils.get_tests_from_file_or_dir(sys.argv[1])
122133
if len(sys.argv) >= 3:
123134
for filename, tests in list(fixtures.items()):
124135
for testname, testdata in list(tests.items()):
125136
if testname == sys.argv[2]:
126137
print("Testing: %s %s" % (filename, testname))
127-
run_block_test(testdata, {'HOMESTEAD_FORK_BLKNUM':0 if 'Homestead' in filename else 5 if 'TestNetwork' in filename else 1000000 })
138+
run_block_test(testdata, {
139+
'HOMESTEAD_FORK_BLKNUM': 0 if 'Homestead' in filename else 5 if 'TestNetwork' in filename
140+
else 1000000})
128141
else:
129142
for filename, tests in list(fixtures.items()):
130143
for testname, testdata in list(tests.items()):
131144
if (filename.split('/')[-1], testname) not in excludes:
132145
print("Testing: %s %s" % (filename, testname))
133-
run_block_test(testdata, {'HOMESTEAD_FORK_BLKNUM':0 if 'Homestead' in filename else 5 if 'TestNetwork' in filename else 1000000 })
134-
else:
135-
fixtures = testutils.get_tests_from_file_or_dir(
136-
os.path.join(testutils.fixture_path, 'BlockchainTests'))
137-
138-
def mk_test_func(filename, testname, testdata):
139-
return lambda: do_test_block(filename, testname, testdata)
140-
141-
for filename, tests in list(fixtures.items()):
142-
for testname, testdata in list(tests.items())[:500]:
143-
func_name = 'test_%s_%s' % (filename, testname)
144-
if (filename.split('/')[-1], testname) not in excludes:
145-
# print 'making function', (filename.split('/')[-1], testname)
146-
globals()[func_name] = mk_test_func(filename, testname, testdata)
146+
run_block_test(testdata, {
147+
'HOMESTEAD_FORK_BLKNUM': 0 if 'Homestead' in filename else 5 if 'TestNetwork' in filename else 1000000})
148+
149+
150+
if __name__ == '__main__':
151+
main()

ethereum/tests/test_contracts.py

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

44
import bitcoin
5+
from secp256k1 import PrivateKey
56
import pytest
67
import serpent
7-
from rlp.utils import decode_hex, encode_hex
8+
from rlp.utils import decode_hex
89

910
from ethereum import tester, utils, abi
1011
from ethereum.slogging import set_level
11-
from ethereum.utils import safe_ord
12+
from ethereum.utils import safe_ord, big_endian_to_int
1213

13-
try:
14-
from c_secp256k1 import ecdsa_sign_raw
15-
except ImportError:
16-
import warnings
17-
warnings.warn('missing c_secp256k1 falling back to pybitcointools')
18-
19-
from bitcoin import ecdsa_raw_sign as ecdsa_sign_raw
2014

2115
# customize VM log output to your needs
2216
# hint: use 'py.test' with the '-s' option to dump logs to the console
@@ -943,7 +937,6 @@ def kall():
943937
944938
"""
945939

946-
import bitcoin
947940

948941

949942
def test_saveload():
@@ -1214,17 +1207,26 @@ def test_ecrecover():
12141207
s = tester.state()
12151208
c = s.abi_contract(ecrecover_code)
12161209

1217-
priv = encode_hex(utils.sha3('some big long brainwallet password'))
1210+
priv = utils.sha3('some big long brainwallet password')
12181211
pub = bitcoin.privtopub(priv)
12191212

1220-
msghash = encode_hex(utils.sha3('the quick brown fox jumps over the lazy dog'))
1221-
V, R, S = ecdsa_sign_raw(msghash, priv)
1213+
msghash = utils.sha3('the quick brown fox jumps over the lazy dog')
1214+
1215+
pk = PrivateKey(priv, raw=True)
1216+
signature = pk.ecdsa_recoverable_serialize(
1217+
pk.ecdsa_sign_recoverable(msghash, raw=True)
1218+
)
1219+
signature = signature[0] + chr(signature[1])
1220+
V = ord(signature[64]) + 27
1221+
R = big_endian_to_int(signature[0:32])
1222+
S = big_endian_to_int(signature[32:64])
1223+
12221224
assert bitcoin.ecdsa_raw_verify(msghash, (V, R, S), pub)
12231225

12241226
addr = utils.big_endian_to_int(utils.sha3(bitcoin.encode_pubkey(pub, 'bin')[1:])[12:])
12251227
assert utils.big_endian_to_int(utils.privtoaddr(priv)) == addr
12261228

1227-
result = c.test_ecrecover(utils.big_endian_to_int(decode_hex(msghash)), V, R, S)
1229+
result = c.test_ecrecover(utils.big_endian_to_int(msghash), V, R, S)
12281230
assert result == addr
12291231

12301232

ethereum/tests/test_keys.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,12 @@
66
logger = get_logger()
77

88

9-
# SETUP TESTS IN GLOBAL NAME SPACE
10-
def gen_func(filename, testname, testdata):
11-
return lambda: do_test_key(filename, testname, testdata)
12-
13-
14-
def do_test_key(filename, testname=None, testdata=None, limit=99999999):
9+
def test_key(filename, testname, testdata,):
1510
logger.debug('running test:%r in %r' % (testname, filename))
1611
assert keys.check_keystore_json(testdata["json"])
1712
privkey = keys.decode_keystore_json(testdata["json"], testdata["password"])
1813
assert utils.encode_hex(privkey) == utils.to_string(testdata["priv"])
1914

20-
fixtures = testutils.get_tests_from_file_or_dir(
21-
os.path.join(testutils.fixture_path, 'KeyStoreTests'))
2215

23-
filenames = sorted(list(fixtures.keys()))
24-
for filename in filenames:
25-
tests = fixtures[filename]
26-
for testname, testdata in list(tests.items()):
27-
func_name = 'test_%s_%s' % (filename, testname)
28-
globals()[func_name] = gen_func(filename, testname, testdata)
16+
def pytest_generate_tests(metafunc):
17+
testutils.generate_test_params('KeyStoreTests', metafunc)

ethereum/tests/test_logging.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ def div(a, b):
6262
except Exception as e:
6363
log.error('an Exception trace should preceed this msg', exc_info=True)
6464
div(1, 0)
65-
assert 'an Exception trace' in caplog.text()
66-
assert 'Traceback' in caplog.text()
65+
assert 'an Exception trace' in caplog.text
66+
assert 'Traceback' in caplog.text
6767
div(1, 1)
68-
assert 'the stack' in caplog.text()
68+
assert 'the stack' in caplog.text
6969

7070

7171
def test_listeners(caplog):
@@ -80,17 +80,17 @@ def log_cb(event_dict):
8080
# activate listener
8181
slogging.log_listeners.append(log_cb) # Add handlers
8282
log.error('test listener', abc='thislistener')
83-
assert 'thislistener' in caplog.text()
83+
assert 'thislistener' in caplog.text
8484
r = called.pop()
8585
assert r == dict(event='test listener', abc='thislistener')
8686

8787
log.trace('trace is usually filtered', abc='thislistener') # this handler for function log_cb does not work
88-
assert "trace is usually filtered" not in caplog.text()
88+
assert "trace is usually filtered" not in caplog.text
8989

9090
# deactivate listener
9191
slogging.log_listeners.remove(log_cb)
9292
log.error('test listener', abc='nolistener')
93-
assert 'nolistener' in caplog.text()
93+
assert 'nolistener' in caplog.text
9494
assert not called
9595

9696

@@ -161,7 +161,7 @@ def test_recorder(caplog):
161161
recorder = slogging.LogRecorder()
162162
assert len(slogging.log_listeners) == 1
163163
log.info('a', v=1)
164-
assert "a" in caplog.text()
164+
assert "a" in caplog.text
165165
r = recorder.pop_records()
166166
assert r[0] == dict(event='a', v=1)
167167
assert len(slogging.log_listeners) == 0
@@ -171,7 +171,7 @@ def test_recorder(caplog):
171171
recorder = slogging.LogRecorder()
172172
assert len(slogging.log_listeners) == 1
173173
log.trace('a', v=2)
174-
assert '"v": 2' in caplog.text()
174+
assert '"v": 2' in caplog.text
175175
r = recorder.pop_records()
176176
assert r[0] == dict(event='a', v=2)
177177
assert len(slogging.log_listeners) == 0
@@ -224,9 +224,9 @@ def test_logger_filter(caplog, logger_name, filter, should_log):
224224
log.addFilter(logging.Filter(filter))
225225
log.info("testlogmessage", v=1)
226226
if should_log:
227-
assert "testlogmessage" in caplog.text()
227+
assert "testlogmessage" in caplog.text
228228
else:
229-
assert "testlogmessage" not in caplog.text()
229+
assert "testlogmessage" not in caplog.text
230230

231231

232232
def test_bound_logger(caplog):
@@ -236,15 +236,15 @@ def test_bound_logger(caplog):
236236
bound_log_1 = real_log.bind(key1="value1")
237237
with caplog.at_level(slogging.TRACE):
238238
bound_log_1.info("test1")
239-
assert "test1" in caplog.text()
240-
assert "key1=value1" in caplog.text()
239+
assert "test1" in caplog.text
240+
assert "key1=value1" in caplog.text
241241

242242
bound_log_2 = bound_log_1.bind(key2="value2")
243243
with caplog.at_level(slogging.TRACE):
244244
bound_log_2.info("test2")
245-
assert "test2" in caplog.text()
246-
assert "key1=value1" in caplog.text()
247-
assert "key2=value2" in caplog.text()
245+
assert "test2" in caplog.text
246+
assert "key1=value1" in caplog.text
247+
assert "key2=value2" in caplog.text
248248

249249

250250
def test_bound_logger_isolation(caplog):

0 commit comments

Comments
 (0)