2008/link-testsuite/harness linktest.py,NONE,1.1

Update of /sources/public/2008/link-testsuite/harness
In directory hutz:/tmp/cvs-serv28821

Added Files:
	linktest.py 
Log Message:
main script for the link test harness
  * python-based
  * developed with unit-testing. run in "sanity" mode after any modification to the code or test cases
  * can be extended to use other link checkers, spiders etc.



--- NEW FILE: linktest.py ---
#!/usr/bin/env python
# encoding: utf-8
"""
link-testsuite/harness/run.py
Run or Generate test suite for link checkers

Created by olivier Thereaux on 2008-01-23.
Copyright (c) 2008 W3C. Licensed under the W3C Software License
http://www.w3.org/Consortium/Legal/copyright-software
"""

import os
import sys
import glob
import getopt
import unittest
import re
import xml.etree.cElementTree as ET

basedir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(basedir, "lib"))
from W3CLinkCheckerClient import W3CLinkCheckerClient, W3CLinkCheckerClient_UT
from LinkTestCase import LinkTestCase, LinkTestCase_UT, LinkTestCollection

help_message = '''

Run or Generate test suite for link checkers

Usage: linktest.py [options] [run|sanity|doc]
    Options: 
        -h, --help: this manual you are reading
        -v, --verbose: verbose output
        -q, --quiet: suppress all output except errors
    
    Modes:
        run: run the link checker test suite 
        sanity: check that this code is still working 
            useful after using test cases or modifying code
        doc: generate an HTML index of the test cases
'''



class Usage(Exception):
    def __init__(self, msg):
        self.msg = msg

class TestRun(unittest.TestCase):
    def test_1_1_readTestCase(self):
        """Opening and parsing a Test Case file (basic)"""
        basedir = getBaseDir()
        test_file = os.path.join(basedir, 'sample', 'sample.test')
        sample_test = readTestCase(test_file)
        self.assert_(isinstance(sample_test, LinkTestCase))

      
    def test_1_2_readTestCase(self):
        """Opening and parsing a Test Case file (values)"""
        basedir = getBaseDir()
        test_file = os.path.join(basedir, 'sample', 'sample.test')
        sample_test = readTestCase(test_file)
        self.assertEqual(
          (sample_test.title,
          sample_test.description, 
          sample_test.docURI, 
          sample_test.runOptions, 
          sample_test.expectResults),
         (u"Sample Test case for a 404", 
         u'''The document links to a 404 not found resource.\n        The checker should report the broken link''',
         "http://qa-dev.w3.org/link-testsuite/http-404.html", 
         {}, 
         {"404": "http://qa-dev.w3.org/link-testsuite/http.php?code=404"})
        )
    
    def test_1_3_readTestCase(self):
        """Ill-formed test case files should throw an exception"""
        basedir = getBaseDir()
        test_file = os.path.join(basedir, 'sample', 'sampleillformed.test')
        self.assertRaises(SyntaxError, readTestCase, test_file)
    
    def test_2_1_readCollectionMeta(self):
        """Opening and parsing a Test Collection metadata file (basic)"""
        basedir = getBaseDir()
        collection_file = os.path.join(basedir, 'sample', 'sample.collection')
        sample_collection = readCollectionMeta(collection_file)
        self.assert_(isinstance(sample_collection, LinkTestCollection))    
    
    def test_2_2_readCollectionMeta(self):
        """Opening and parsing a Test Collection metadata file (values)"""
        basedir = getBaseDir()
        collection_file = os.path.join(basedir, 'sample', 'sample.collection')
        sample_collection = readCollectionMeta(collection_file)
        self.assertEqual(
          (sample_collection.title, sample_collection.description, sample_collection.cases),
          (u"test", u"Sample Collection with one test", ["sample.test"])
        )
    
    def test_3_buildTestCollection(self):
        """Test building a Test Collection"""
        basedir = getBaseDir()
        collection_file = os.path.join(basedir, 'sample', 'sample.collection')
        sample_collection = readCollectionMeta(collection_file)
        for testcase_file in sample_collection.cases:
            test_file = os.path.join(basedir, 'sample', testcase_file)
            sample_test = readTestCase(test_file)
            self.assertEqual(
              (sample_test.title,
               sample_test.description,
              sample_test.docURI, 
              sample_test.runOptions, 
              sample_test.expectResults),
              (u"Sample Test case for a 404", 
              u'''The document links to a 404 not found resource.\n        The checker should report the broken link''',
              "http://qa-dev.w3.org/link-testsuite/http-404.html", 
              {}, 
              {"404": "http://qa-dev.w3.org/link-testsuite/http.php?code=404"})
             )
    
    def test_4_buildTestSuite(self):
        """Test building the whole Link Checker Test Suite"""
        suite = buildTestSuite()
        pass

def main(argv=None):
    verbose=1
    if argv is None:
        argv = sys.argv
    try:
        try:
            opts, args = getopt.getopt(argv[1:], "ho:vq", ["help", "verbose", "quiet"])
            for (opt, value) in opts:
                if opt == "h" or opt == "--help":
                    raise Usage(msg)
        except getopt.error, msg:
            raise Usage(msg)
    
        # option processing
        for option, value in opts:
            if option == "-v" or opt == "--verbose":
                verbose = 2
            if option == "-q" or opt == "--quiet":
                verbose = 0
            if option in ("-h", "--help"):
                raise Usage(help_message)
            if option in ("-o", "--output"):
                output = value

        if len(args) == 0:
            raise Usage(help_message)
    
    except Usage, err:
        print >> sys.stderr, sys.argv[0].split("/")[-1] + ": " + str(err.msg)
        print >> sys.stderr, "\t for help use --help"
        return 2

        
    if args[0] == "run":
        suite = buildTestSuite()
        unittest.TextTestRunner(verbosity=verbose).run(suite)
    elif args[0] == "sanity":
        suite1 = unittest.TestLoader().loadTestsFromTestCase(W3CLinkCheckerClient_UT)
        suite2 = unittest.TestLoader().loadTestsFromTestCase(LinkTestCase_UT)
        suite3 = unittest.TestLoader().loadTestsFromTestCase(TestRun)
        suite = unittest.TestSuite([suite1, suite2, suite3])
        unittest.TextTestRunner(verbosity=verbose).run(suite)
        
    elif args[0] == "doc":
        pass

def getBaseDir():
    basedir = os.path.dirname(os.path.abspath(__file__))
    return basedir

def readTestCase(test_file):
    test_file_handle = open(test_file, 'r')
    try:
        tree = ET.parse(test_file_handle)
    except SyntaxError, v:
        raise v
    title = tree.findtext("title")
    if title:
        title = title.decode("utf-8")
    else:
        title = None
    descr = tree.findtext("description")
    if descr:
        descr = descr.decode("utf-8")
    else:
        descr = None
    doc_elt = tree.find(".//doc")
    if doc_elt.attrib.has_key("href"):
        test_uri = doc_elt.attrib["href"]
    else:
        test_uri = None
    options_elt = tree.find(".//options")
    ## TODO
    expect_elt = tree.find(".//expect")
    expected = dict()
    if expect_elt.getchildren():
        for child in expect_elt.findall("report"):
            if child.attrib.has_key("href") and child.attrib.has_key("code"):
                if expected.has_key(child.tag):
                    expected[child.attrib["code"]].append(child.attrib["href"])
                else:
                    expected[child.attrib["code"]] = (child.attrib["href"])
                
    case = LinkTestCase(title=title, description=descr, docURI=test_uri, runOptions=None, expectResults=expected)
    return case

def readTestCollection(collection_path):
    basedir = getBaseDir()
    
    for metafile in glob.glob(os.path.join(collection_path, '*.collection')):
        collection = readCollectionMeta(metafile)
    for testfile in (glob.glob(os.path.join(collection_path, '*.test'))):
        case = readTestCase(test_file)
        collection.cases

def readCollectionMeta(collection_file):
    collection_file_handle = open(collection_file, 'r')
    try:
        tree = ET.parse(collection_file_handle)
    except SyntaxError, v:
        raise v
    title = tree.findtext("title")
    if title:
        title = title.decode("utf-8")
    else:
        title = u""
    description = tree.findtext("description")
    if description:
        description = description.decode("utf-8")
    else:
        description = u""
    tests = list()
    for test in tree.findall("testcase"):
        if test.attrib.has_key("src"):
            tests.append(test.attrib["src"])
        else:
            tests.append(test.tag)
    collection = LinkTestCollection(title=title, description=description, cases=tests)
    return collection

def buildTestSuite():
    suite = unittest.TestSuite()
    basedir = getBaseDir()
    for testcollection_file in (glob.glob(os.path.join(basedir, 'testcases', '**', '*.collection'))):
        colldir = os.path.dirname(os.path.abspath(testcollection_file))
        colldir = os.path.split(colldir)[-1]
        
        testcollection = readCollectionMeta(testcollection_file)
        for testfile in testcollection.cases:
            case = readTestCase(os.path.join(basedir, 'testcases',colldir,testfile))
            suite.addTest(case)
    return suite


if __name__ == "__main__":
    sys.exit(main())

Received on Monday, 28 January 2008 02:19:44 UTC