Skip to content

Parser Testing

Paul McGuire edited this page Jan 19, 2020 · 5 revisions

Pyparsing 3.0 will introduce some new methods in a pyparsing_testing namespace, to add assertions for easy validation of ParseResults and ParseExceptions. The API is still undergoing some revisions, but hopefully these methods will make it easier to write pyparsing parser unit tests using a consistent style and test API.

(These classes were backported to pyparsing 2.4.6 so that you can write automated unit tests of your parsing apps before upgrading to pyparsing 3.0.)

  • Assert methods defined as part of the pyparsing_test.TestParseResultsAsserts mixin class. Add these methods to your own unittest.TestCase classes using:

    from pyparsing import pyparsing_test as ppt
    class MyTestCase(ppt.TestParseResultsAsserts, unittest.TestCase):
        ... test case class body ...
    

    Assert methods:

    • assertParseResultsEquals(self, result, expected_list=None, expected_dict=None, msg=None)

      General purpose assert that can compare the ParseResults object returned from calling parseString, searchString or scanString to an expected list and/or expected dict.

    • assertParseAndCheckList(self, expr, test_string, expected_list, msg=None, verbose=True)

      Convenience method to take a pyparsing expression and test string, run expr.parseString(test_string), and compare the parsed tokens to an expected list. If verbose is True, the parsed results are shown using dump().

    • assertParseAndCheckDict(self, expr, test_string, expected_dict, msg=None, verbose=True)

      Convenience method to take a pyparsing expression and test string, run expr.parseString(test_string), and compare the named results to an expected dict. If verbose is True, the parsed results are shown using dump().

    • assertRunTestResults(self, run_tests_report, expected_parse_results=None, msg=None)

      Unit test assertion to evaluate output of ParserElement.runTests(). If a list of list-dict tuples is given as the expected_parse_results argument, then these are zipped with the report tuples returned by runTests and evaluated using assertParseResultsEquals. Finally, asserts that the overall runTests() success value is True.

    • assertRaisesParseException(self, exc_type=ParseException, msg=None)

      Contexgt manager to be used for negative test cases that should raise a ParseException, similar to unittest.TestCase.assertRaises.

  • reset_pyparsing_context context manager, to restore pyparsing config settings

    Context manager to be used when writing unit tests that modify pyparsing config values:

    • packrat parsing
    • default whitespace characters.
    • default keyword characters
    • literal string auto-conversion class
    • __diag__ settings

    Example:

        with reset_pyparsing_context():
            # temporarily change literal-inlining to use Suppress instead of Literal
            ParserElement.inlineLiteralsUsing(Suppress)