How to use report_testcases method in Behave

Best Python code snippet using behave

Run Behave automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

behave.junit_filter.py

Source: behave.junit_filter.py Github

copy
1#!/usr/bin/env python
2"""
3Filter JUnit XML reports to show only a subset of all information.
4
5JUNIT XML SCHEMA:
6
7    element:testsuite: failures="..." errors="..."
8        +-- element:testcase: name="..." status="..."
9"""
10
11from __future__ import absolute_import, print_function, with_statement
12import os.path
13import sys
14import argparse
15from fnmatch import fnmatch
16from xml.etree import ElementTree as ET
17from xml.dom import minidom
18from behave.textutil import indent
19
20__author = "Jens Engel"
21__status__ = "prototype"
22
23NAME = os.path.basename(__file__)
24VERSION = "0.1.0"
25REPORT_DIR = "reports"
26
27def xml_prettify(elem):
28    """Return a pretty-printed XML string for the XML element."""
29    text = ET.tostring(elem, "utf-8")
30    reparsed = minidom.parseString(text)
31    return reparsed.toprettyxml(indent=" ")
32
33def xml_select_testcases_with_status(tree, status):
34    return tree.findall(".//testcase[@status='%s']" % status)
35
36def path_select_files(paths, pattern="*.xml"):
37    if not paths:
38        paths = [REPORT_DIR]
39
40    selected = []
41    for pathname in paths:
42        if os.path.isdir(pathname):
43            for root, dirs, files in os.walk(pathname):
44                for filename in files:
45                    if fnmatch(filename, pattern):
46                        filename2 = os.path.join(root, filename)
47                        selected.append(os.path.normpath(filename2))
48        elif os.path.isfile(pathname) and fnmatch(pathname, pattern):
49            selected.append(pathname)
50    return selected
51
52def report_testcases(filename, testcases):
53    print(u"REPORT: {0}".format(filename))
54    for xml_testcase in testcases:
55        print("  TESTCASE: {0}".format(xml_testcase.get("name")))
56        xml_text = indent(xml_prettify(xml_testcase), "    ")
57        print(xml_text)
58
59def main(args=None):
60    """Filter JUnit XML reports to show only a subset of all information."""
61    if args is None:
62        args = sys.argv[1:]
63
64    parser = argparse.ArgumentParser(prog=NAME,
65                                     description=main.__doc__)
66    parser.add_argument("-s", "--status", default="failed", required=False,
67                        choices=["passed", "failed", "skipped"],
68                        help="Status to select (passed, failed, skipped).")
69    parser.add_argument("xml_file", nargs="*",
70                        help="XML file(s) or directory with XML files.")
71    parser.add_argument("--version", action="version", version=VERSION)
72
73    options = parser.parse_args(args)
74
75    xml_files = options.xml_file
76    xml_reports = path_select_files(xml_files)
77    for xml_filename in xml_reports:
78        tree = ET.parse(xml_filename)
79        testcases = xml_select_testcases_with_status(tree, options.status)
80        if testcases:
81            report_testcases(xml_filename, testcases)
82    return 0
83
84if __name__ == "__main__":
85    sys.exit(main())
86
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run Python Tests on LambdaTest Cloud Grid

Execute automation tests with Behave on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)