test: use unittest for test_framework unit testing #18576

pull glozow wants to merge 1 commits into bitcoin:master from glozow:framework-unittests changing 3 files +39 −46
  1. glozow commented at 6:20 PM on April 9, 2020: member

    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).

  2. DrahtBot added the label Tests on Apr 9, 2020
  3. DrahtBot commented at 9:07 PM on April 9, 2020: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    No conflicts as of last run.

  4. DrahtBot cross-referenced this on Apr 9, 2020 from issue Implement BIP 340-342 validation (Schnorr/taproot/tapscript) by sipa
  5. glozow force-pushed on Apr 10, 2020
  6. glozow force-pushed on Apr 10, 2020
  7. glozow force-pushed on Apr 10, 2020
  8. fanquake renamed this:
    wip [test] use unittest for test_framework unit testing
    [wip] test: use unittest for test_framework unit testing
    on Apr 11, 2020
  9. fanquake requested review from jnewbery on Apr 11, 2020
  10. jnewbery commented at 12:30 AM on April 11, 2020: member

    Concept ACK! @sipa @MarcoFalke you may also be interested in this, since we all discussed this in #18378.

  11. glozow force-pushed on Apr 11, 2020
  12. glozow force-pushed on Apr 11, 2020
  13. glozow force-pushed on Apr 11, 2020
  14. DrahtBot cross-referenced this on Apr 12, 2020 from issue Native Descriptor Wallets using DescriptorScriptPubKeyMan by achow101
  15. in test/functional/test_runner.py:28 in 6cc896405e outdated
      23 | @@ -24,6 +24,10 @@
      24 |  import tempfile
      25 |  import re
      26 |  import logging
      27 | +import unittest
      28 | +import test_framework.script
    


    jnewbery commented at 8:40 PM on April 13, 2020:

    It seems a shame to have to list the modules out multiple times. If you use the loadTestsFromName() method, you can avoid having to import the modules:

    TEST_FRAMEWORK_MODULES = [
        "script",
        "key",
        "messages",
    ]
    ... 
        # Test Framework Tests
        for module in TEST_FRAMEWORK_MODULES:
            suite = unittest.TestLoader().loadTestsFromName("test_framework.{}".format(module))
            unittest.TextTestRunner(verbosity=1).run(suite)
    

    If you wanted to be really clever, you could just search all the modules for which ones import unittest and then automatically pick them up in the test runner, so they don't need to be listed out at all. I don't think that's necessary in this PR, but you might want to consider it in a follow-up.


    glozow commented at 2:31 AM on April 21, 2020:

    Ahhh yes this is what I wanted to do! Ran into a bunch of import errors when I tried to auto discover everything, will look into it further.

  16. in test/functional/test_runner.py:399 in 6cc896405e outdated
     392 | @@ -384,6 +393,11 @@ def run_tests(*, test_list, src_dir, build_dir, tmpdir, jobs=1, enable_coverage=
     393 |      if os.path.isdir(cache_dir):
     394 |          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))
     395 |  
     396 | +    # Test Framework Tests
     397 | +    for module in TEST_FRAMEWORK_MODULES:
     398 | +        suite = unittest.TestLoader().loadTestsFromModule(module)
     399 | +        unittest.TextTestRunner(verbosity=2).run(suite)
    


    jnewbery commented at 8:42 PM on April 13, 2020:

    It'd be nice if we could have less verbosity for successful unittest runs, and only have verbose output for tests that fail. Otherwise, these will take up a lot of screen space for every run.


    glozow commented at 5:27 PM on April 21, 2020:

    Good point, thank you so much for all the feedback! Verbosity=1 does the least printing, but still prints every time TestCase.run() is called, so I made it more concise by grouping all the tests into one TestSuite and calling run() just once instead of doing them each individually. These tests would probably all be very small so I figured it would be alright. On success, it's 4 lines. On fail, it's more verbose and quits early. Hopefully this is ok?

  17. jnewbery commented at 8:45 PM on April 13, 2020: member

    Concept and approach ACK. Thanks for tackling this, @gzhao408 .

    I left a couple of comments inline. A few more ideas:

    • Perhaps print something immediately before the unit tests to indicate what's happening. Something like "Running test framework unit tests".
    • The test runner should probably exit with an error state if any of the unit tests fail (to cause travis to fail immediately).
  18. jnewbery commented at 11:09 PM on April 13, 2020: member

    Here are some tips for writing great commit messages: https://chris.beams.io/posts/git-commit/. Particularly look at section 7 (why-not-how), and make sure to include a blank line between the summary and details.

  19. practicalswift commented at 3:05 PM on April 21, 2020: contributor

    Concept ACK

    Thanks for tackling this. Great initiative! :)

  20. jnewbery commented at 6:12 PM on April 21, 2020: member

    This is looking really good @gzhao408 . I recommend that in this PR you just add the unit test framework to test_runner.py and move the script tests to script.py, and then open follow-up PRs to add the key.py and messages.py tests.

  21. glozow force-pushed on Apr 21, 2020
  22. glozow renamed this:
    [wip] test: use unittest for test_framework unit testing
    test: use unittest for test_framework unit testing
    on Apr 21, 2020
  23. glozow marked this as ready for review on Apr 21, 2020
  24. glozow force-pushed on Apr 21, 2020
  25. 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
    de8905adf2
  26. glozow force-pushed on Apr 26, 2020
  27. glozow commented at 8:34 PM on April 26, 2020: member

    Ready for Review if anyone wants to take a look :) addressed jnewbery's comments.

    It looks like travis failed on wallet_bumpfee.py on the last push. I just passed it locally and can't figure out why it's failing, so I'm having it run again. Will look again in a bit...

  28. jnewbery commented at 11:55 PM on April 29, 2020: member

    Tested ACK de8905adf204c42bba810802f82b98f7b3dd26dc. Great stuff gzhao408 . Thanks for this!

    For your next PR, try to summarize the why instead of the how in your commit log. People can look at the code to see what changes you've made, such as which files you've deleted. The log should summarize and give motivation.

  29. glozow commented at 11:58 PM on April 29, 2020: member

    Thanks so much for the review @jnewbery!

    For your next PR, try to summarize the why instead of the how in your commit log. People can look at the code to see what changes you've made, such as which files you've deleted. The log should summarize and give motivation.

    Ah, gotcha! Thanks for the feedback :) will do.

  30. MarcoFalke merged this on Apr 30, 2020
  31. MarcoFalke closed this on Apr 30, 2020

  32. sidhujag referenced this in commit 945eebe383 on May 2, 2020
  33. MarcoFalke cross-referenced this on May 13, 2020 from issue tests: implement base58_decode by 10xcryptodev
  34. MarcoFalke cross-referenced this on May 13, 2020 from issue test: Make scriptnum test a unit test by MarcoFalke
  35. glozow deleted the branch on May 25, 2020
  36. MarcoFalke cross-referenced this on May 27, 2020 from issue test: Moved the CScriptNum asserts into the unit test in script.py by gillichu
  37. gillichu referenced this in commit 8a04208077 on May 27, 2020
  38. gillichu referenced this in commit 9d5dd8752a on May 27, 2020
  39. gillichu referenced this in commit 48d08c0899 on May 28, 2020
  40. gillichu referenced this in commit 74b863e51c on May 28, 2020
  41. gillichu referenced this in commit c368d241da on May 28, 2020
  42. gillichu referenced this in commit 401487d24e on May 28, 2020
  43. gillichu referenced this in commit ef4cce175b on May 31, 2020
  44. gillichu referenced this in commit 58c4a30b0d on May 31, 2020
  45. gillichu referenced this in commit 63cba4b7fe on Jun 1, 2020
  46. gillichu referenced this in commit 91c08205bc on Jun 2, 2020
  47. gillichu referenced this in commit 60927840ff on Jun 2, 2020
  48. gillichu referenced this in commit 7daffc6a90 on Jun 3, 2020
  49. laanwj referenced this in commit 39afe5b1c6 on Jun 4, 2020
  50. sidhujag referenced this in commit f5a1bed93b on Jun 4, 2020
  51. jnewbery cross-referenced this on Jun 9, 2020 from issue Add Muhash3072 implementation in Python by fjahr
  52. stackman27 referenced this in commit 35dc414588 on Jun 26, 2020
  53. Fabcien referenced this in commit 4e33fac0c9 on Jan 26, 2021
  54. Fabcien referenced this in commit 2a3ba49ebb on Mar 1, 2021
  55. bitcoin locked this on Feb 15, 2022

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-05-20 06:54 UTC