Skip to content

Commit a66ba6d

Browse files
author
MarcoFalke
committed
Merge #18576: test: use unittest for test_framework unit testing
de8905a test: use unittest and test_runner for test framework unit testing (Gloria Zhao) Pull request description: Proposal for unit testing on test_framework functions: 1. Use the python `unittest` library. Don't use test_framework to test itself. 2. Put the tests inside the same file as the functions they are testing. 3. Call the tests from `test_runner.py`. To include more Test Framework tests, add the filename to the list `TEST_FRAMEWORK_MODULES`. Don't add new files or change the list of accepted script prefixes. Makes these changes for `bn2vch` (followup to [this comment](bitcoin/bitcoin#18378 (review))). ACKs for top commit: jnewbery: Tested ACK de8905a. Great stuff gzhao408 . Thanks for this! Tree-SHA512: 91572d43e203a1864765b93a9472667994115ec38b271f2b2f9fcd0f0112b393fc24ba7d2371d5a34b0a6a4522f6b934fc5164363819aa7ed8d6c6c9a60cc101
2 parents 00c1a4d + de8905a commit a66ba6d

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,
@@ -708,3 +709,25 @@ def SegwitV0SignatureHash(script, txTo, inIdx, hashtype, amount):
708709
ss += struct.pack("<I", hashtype)
709710

710711
return hash256(ss)
712+
713+
class TestFrameworkScript(unittest.TestCase):
714+
def test_bn2vch(self):
715+
self.assertEqual(bn2vch(0), bytes([]))
716+
self.assertEqual(bn2vch(1), bytes([0x01]))
717+
self.assertEqual(bn2vch(-1), bytes([0x81]))
718+
self.assertEqual(bn2vch(0x7F), bytes([0x7F]))
719+
self.assertEqual(bn2vch(-0x7F), bytes([0xFF]))
720+
self.assertEqual(bn2vch(0x80), bytes([0x80, 0x00]))
721+
self.assertEqual(bn2vch(-0x80), bytes([0x80, 0x80]))
722+
self.assertEqual(bn2vch(0xFF), bytes([0xFF, 0x00]))
723+
self.assertEqual(bn2vch(-0xFF), bytes([0xFF, 0x80]))
724+
self.assertEqual(bn2vch(0x100), bytes([0x00, 0x01]))
725+
self.assertEqual(bn2vch(-0x100), bytes([0x00, 0x81]))
726+
self.assertEqual(bn2vch(0x7FFF), bytes([0xFF, 0x7F]))
727+
self.assertEqual(bn2vch(-0x8000), bytes([0x00, 0x80, 0x80]))
728+
self.assertEqual(bn2vch(-0x7FFFFF), bytes([0xFF, 0xFF, 0xFF]))
729+
self.assertEqual(bn2vch(0x80000000), bytes([0x00, 0x00, 0x00, 0x80, 0x00]))
730+
self.assertEqual(bn2vch(-0x80000000), bytes([0x00, 0x00, 0x00, 0x80, 0x80]))
731+
self.assertEqual(bn2vch(0xFFFFFFFF), bytes([0xFF, 0xFF, 0xFF, 0xFF, 0x00]))
732+
self.assertEqual(bn2vch(123456789), bytes([0x15, 0xCD, 0x5B, 0x07]))
733+
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
@@ -237,7 +242,6 @@
237242
'rpc_help.py',
238243
'feature_help.py',
239244
'feature_shutdown.py',
240-
'framework_test_script.py',
241245
# Don't append tests at the end to avoid merge conflicts
242246
# Put them in a random line within the section that fits their approximate run-time
243247
]
@@ -400,6 +404,16 @@ def run_tests(*, test_list, src_dir, build_dir, tmpdir, jobs=1, enable_coverage=
400404
if os.path.isdir(cache_dir):
401405
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))
402406

407+
# Test Framework Tests
408+
print("Running Unit Tests for Test Framework Modules")
409+
test_framework_tests = unittest.TestSuite()
410+
for module in TEST_FRAMEWORK_MODULES:
411+
test_framework_tests.addTest(unittest.TestLoader().loadTestsFromName("test_framework.{}".format(module)))
412+
result = unittest.TextTestRunner(verbosity=1, failfast=True).run(test_framework_tests)
413+
if not result.wasSuccessful():
414+
logging.debug("Early exiting after failure in TestFramework unit tests")
415+
sys.exit(False)
416+
403417
tests_dir = src_dir + '/test/functional/'
404418

405419
flags = ['--cachedir={}'.format(cache_dir)] + args
@@ -623,7 +637,7 @@ def was_successful(self):
623637
def check_script_prefixes():
624638
"""Check that test scripts start with one of the allowed name prefixes."""
625639

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

629643
if bad_script_names:

0 commit comments

Comments
 (0)