Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork298
Parser Testing
Pyparsing 3.0 will introduce some new methods in apyparsing_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.TestParseResultsAssertsmixin class. Add these methods to yourown unittest.TestCase classes using:from pyparsing import pyparsing_test as pptclass 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
ParseResultsobject returned from callingparseString,searchStringorscanStringto 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. Ifverboseis True, the parsed results are shownusingdump().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. Ifverboseis True, the parsed results are shownusingdump().assertRunTestResults(self, run_tests_report, expected_parse_results=None, msg=None)Unit test assertion to evaluate output of
ParserElement.runTests(). If a list oflist-dict tuples is given as theexpected_parse_resultsargument, then these are zippedwith the report tuples returned by runTests and evaluated usingassertParseResultsEquals.Finally, asserts that the overallrunTests()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 tounittest.TestCase.assertRaises.
reset_pyparsing_contextcontext manager, to restore pyparsingconfig settingsContext 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)