Skip to content

Commit

Permalink
Extract test name and class name from test description to use in Test…
Browse files Browse the repository at this point in the history
…Case

In order to allow a better formatting of the junit XML result and not
hardcode None into classname attribute if we can avoid it.

Add also a test with test description contains '.'

Signed-off-by: Frederic Martinsons <frederic.martinsons@sigfox.com>
  • Loading branch information
fmartinsons committed May 9, 2021
1 parent 0cfd448 commit 0d63aff
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
23 changes: 22 additions & 1 deletion tap2junit/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,32 @@
from tap2junit.tap13 import TAP13 as tap13


def extract_test_info_from_description(description):
# If test description is path-like (contains /), consider the last
# token to be the test name and convert the dirname to classname.
# The test description can also have already a class name style
# (module serapated by '.') so do the same for this case
if description and "/" in description:
sanitize = os.path.normpath(description)
(test_class, test_name) = os.path.split(sanitize)
# Remove possible heading slash and replace / by .
test_class = test_class.lstrip("/").replace("/", ".")
elif description and "." in description:
# Remove possible heading slash and replace / by .
test_name = description.split(".")[-1]
test_class = ".".join(description.split(".")[0:-1])
else:
test_name = description
test_class = None
return (test_class, test_name)


def map_yaml_to_junit(test):
yaml = test.yaml or {}
# Even though the name is `duration_ms` the value is in seconds.
elapsed_sec = yaml.get("duration_ms", None)
t = TestCase(test.description, classname=None, elapsed_sec=elapsed_sec)
(test_class, test_name) = extract_test_info_from_description(test.description)
t = TestCase(test_name, classname=test_class, elapsed_sec=elapsed_sec)
if test.result == "ok":
if test.directive in ("SKIP", "TODO"):
t.add_skipped_info(test.comment)
Expand Down
12 changes: 12 additions & 0 deletions test/fixtures/test4.tap
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
TAP version 13
1..9
ok 1 myplugin.module1.test1
ok 2 myplugin.module1.test2
ok 3 myplugin.module2.test1
ok 4 myplugin.module2.test2
ok 5 myplugin.module2.testSub.test1
ok 6 myplugin.module2.testSub.test2
ok 7 myplugin.test1
not ok 8 myplugin.test2
ok 9 myplugin.test3

0 comments on commit 0d63aff

Please sign in to comment.