Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6332831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:21:20+00:00 2026-05-24T18:21:20+00:00

I’m writing a module that involves parsing html for data and creating an object

  • 0

I’m writing a module that involves parsing html for data and creating an object from it. Basically, I want to create a set of testcases where each case is an html file paired with a golden/expected pickled object file.

As I make changes to the parser, I would like to run this test suite to ensure that each html page is parsed to equal the ‘golden’ file (essentially a regression suite)

I can see how to code this as a single test case, where I would load all file pairs from some directory and then iterate through them. But I believe this would end up being reported as a single test case, pass or fail. But I want a report that says, for example, 45/47 pages parsed successfully.

How do I arrange this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T18:21:21+00:00Added an answer on May 24, 2026 at 6:21 pm

    I’ve done similar things with the unittest framework by writing a function which creates and returns a test class. This function can then take in whatever parameters you want and customise the test class accordingly. You can also customise the __doc__ attribute of the test function(s) to get customised messages when running the tests.

    I quickly knocked up the following example code to illustrate this. Instead of doing any actual testing, it uses the random module to fail some tests for demonstration purposes. When created, the classes are inserted into the global namespace so that a call to unittest.main() will pick them up. Depending on how you run your tests, you may wish to do something different with the generated classes.

    import os
    import unittest
    
    # Generate a test class for an individual file.
    def make_test(filename):
        class TestClass(unittest.TestCase):
            def test_file(self):
                # Do the actual testing here.
                # parsed = do_my_parsing(filename)
                # golden = load_golden(filename)
                # self.assertEquals(parsed, golden, 'Parsing failed.')
    
                # Randomly fail some tests.
                import random
                if not random.randint(0, 10):
                    self.assertEquals(0, 1, 'Parsing failed.')
    
            # Set the docstring so we get nice test messages.
            test_file.__doc__ = 'Test parsing of %s' % filename
    
        return TestClass
    
    # Create a single file test.
    Test1 = make_test('file1.html')
    
    # Create several tests from a list.
    for i in range(2, 5):
        globals()['Test%d' % i] = make_test('file%d.html' % i)
    
    # Create them from a directory listing.
    for dirname, subdirs, filenames in os.walk('tests'):
        for f in filenames:
            globals()['Test%s' % f] = make_test('%s/%s' % (dirname, f))
    
    # If this file is being run, run all the tests.
    if __name__ == '__main__':
        unittest.main()
    

    A sample run:

    $ python tests.py -v
    Test parsing of file1.html ... ok
    Test parsing of file2.html ... ok
    Test parsing of file3.html ... ok
    Test parsing of file4.html ... ok
    Test parsing of tests/file5.html ... ok
    Test parsing of tests/file6.html ... FAIL
    Test parsing of tests/file7.html ... ok
    Test parsing of tests/file8.html ... ok
    
    ======================================================================
    FAIL: Test parsing of tests/file6.html
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "generic.py", line 16, in test_file
        self.assertEquals(0, 1, 'Parsing failed.')
    AssertionError: Parsing failed.
    
    ----------------------------------------------------------------------
    Ran 8 tests in 0.004s
    
    FAILED (failures=1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I have a French site that I want to parse, but am running into
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
i got an object with contents of html markup in it, for example: string
I'm trying to create an if statement in PHP that prevents a single post
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.