Skip to content

Commit de8905a

Browse files
committed
test: use unittest and test_runner for test framework unit testing
Test the test_framework, but don't use test_framework objects or functions to test itself Use python unittest library and put test_framework's unit tests inside their respective files Add the filename to TEST_FRAMEWORK_MODULES in test_runner Aggregate all test_framework tests into one TestSuite to run before the functional tests in test_runner Delete framework_test_script, move test_bn2vch to script.py and add to TEST_FRAMEWORK_MODULES in test_runner
1 parent 3e50fdb commit de8905a

File tree

3 files changed

+39
-46
lines changed

3 files changed

+39
-46
lines changed

test/functional/framework_test_script.py

Lines changed: 0 additions & 44 deletions
This file was deleted.

test/functional/test_framework/script.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"""
99
import hashlib
1010
import struct
11+
import unittest
1112

1213
from .messages import (
1314
CTransaction,
@@ -715,3 +716,25 @@ def SegwitV0SignatureHash(script, txTo, inIdx, hashtype, amount):
715716
ss += struct.pack("<I", hashtype)
716717

717718
return hash256(ss)
719+
720+
class TestFrameworkScript(unittest.TestCase):
721+
def test_bn2vch(self):
722+
self.assertEqual(bn2vch(0), bytes([]))
723+
self.assertEqual(bn2vch(1), bytes([0x01]))
724+
self.assertEqual(bn2vch(-1), bytes([0x81]))
725+
self.assertEqual(bn2vch(0x7F), bytes([0x7F]))
726+
self.assertEqual(bn2vch(-0x7F), bytes([0xFF]))
727+
self.assertEqual(bn2vch(0x80), bytes([0x80, 0x00]))
728+
self.assertEqual(bn2vch(-0x80), bytes([0x80, 0x80]))
729+
self.assertEqual(bn2vch(0xFF), bytes([0xFF, 0x00]))
730+
self.assertEqual(bn2vch(-0xFF), bytes([0xFF, 0x80]))
731+
self.assertEqual(bn2vch(0x100), bytes([0x00, 0x01]))
732+
self.assertEqual(bn2vch(-0x100), bytes([0x00, 0x81]))
733+
self.assertEqual(bn2vch(0x7FFF), bytes([0xFF, 0x7F]))
734+
self.assertEqual(bn2vch(-0x8000), bytes([0x00, 0x80, 0x80]))
735+
self.assertEqual(bn2vch(-0x7FFFFF), bytes([0xFF, 0xFF, 0xFF]))
736+
self.assertEqual(bn2vch(0x80000000), bytes([0x00, 0x00, 0x00, 0x80, 0x00]))
737+
self.assertEqual(bn2vch(-0x80000000), bytes([0x00, 0x00, 0x00, 0x80, 0x80]))
738+
self.assertEqual(bn2vch(0xFFFFFFFF), bytes([0xFF, 0xFF, 0xFF, 0xFF, 0x00]))
739+
self.assertEqual(bn2vch(123456789), bytes([0x15, 0xCD, 0x5B, 0x07]))
740+
self.assertEqual(bn2vch(-54321), bytes([0x31, 0xD4, 0x80]))

test/functional/test_runner.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import tempfile
2525
import re
2626
import logging
27+
import unittest
2728

2829
# Formatting. Default colors to empty strings.
2930
BOLD, GREEN, RED, GREY = ("", ""), ("", ""), ("", ""), ("", "")
@@ -65,6 +66,10 @@
6566
TEST_EXIT_PASSED = 0
6667
TEST_EXIT_SKIPPED = 77
6768

69+
TEST_FRAMEWORK_MODULES = [
70+
"script",
71+
]
72+
6873
EXTENDED_SCRIPTS = [
6974
# These tests are not run by default.
7075
# Longest test should go first, to favor running tests in parallel
@@ -221,7 +226,6 @@
221226
'rpc_help.py',
222227
'feature_help.py',
223228
'feature_shutdown.py',
224-
'framework_test_script.py',
225229
# Don't append tests at the end to avoid merge conflicts
226230
# Put them in a random line within the section that fits their approximate run-time
227231
]
@@ -384,6 +388,16 @@ def run_tests(*, test_list, src_dir, build_dir, tmpdir, jobs=1, enable_coverage=
384388
if os.path.isdir(cache_dir):
385389
print("%sWARNING!%s There is a cache directory here: %s. If tests fail unexpectedly, try deleting the cache directory." % (BOLD[1], BOLD[0], cache_dir))
386390

391+
# Test Framework Tests
392+
print("Running Unit Tests for Test Framework Modules")
393+
test_framework_tests = unittest.TestSuite()
394+
for module in TEST_FRAMEWORK_MODULES:
395+
test_framework_tests.addTest(unittest.TestLoader().loadTestsFromName("test_framework.{}".format(module)))
396+
result = unittest.TextTestRunner(verbosity=1, failfast=True).run(test_framework_tests)
397+
if not result.wasSuccessful():
398+
logging.debug("Early exiting after failure in TestFramework unit tests")
399+
sys.exit(False)
400+
387401
tests_dir = src_dir + '/test/functional/'
388402

389403
flags = ['--cachedir={}'.format(cache_dir)] + args
@@ -607,7 +621,7 @@ def was_successful(self):
607621
def check_script_prefixes():
608622
"""Check that test scripts start with one of the allowed name prefixes."""
609623

610-
good_prefixes_re = re.compile("^(example|feature|interface|mempool|mining|p2p|rpc|wallet|tool|framework_test)_")
624+
good_prefixes_re = re.compile("^(example|feature|interface|mempool|mining|p2p|rpc|wallet|tool)_")
611625
bad_script_names = [script for script in ALL_SCRIPTS if good_prefixes_re.match(script) is None]
612626

613627
if bad_script_names:

0 commit comments

Comments
 (0)