From cfcd8cf67f33198c059673ea1e34c15bccfa6fcc Mon Sep 17 00:00:00 2001 From: Jaime Soriano Pastor Date: Thu, 19 Mar 2020 20:48:55 +0100 Subject: [PATCH] Add support of TEST_TAGS in python tests (#17075) (#17122) Add a `tag(tag)` decorator that skips a test if the tag is not included in the comma-separated list of `TEST_TAGS` environment variable. This offers an initial support for the similar implementation added for mage in #16937. (cherry picked from commit 3bf05f51c23ec33fc12871c544415cf638d04633) --- libbeat/tests/system/beat/tags.py | 25 +++++++++++++++++++++++++ metricbeat/tests/system/metricbeat.py | 3 ++- 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 libbeat/tests/system/beat/tags.py diff --git a/libbeat/tests/system/beat/tags.py b/libbeat/tests/system/beat/tags.py new file mode 100644 index 00000000000..6bcb33bffd5 --- /dev/null +++ b/libbeat/tests/system/beat/tags.py @@ -0,0 +1,25 @@ +import os +import unittest + + +def tag(tag): + """ + Decorates a test function with a tag following go build tags semantics, + if the tag is not included in TEST_TAGS environment variable, the test is + skipped. + TEST_TAGS can be a comma-separated list of tags, e.g: TEST_TAGS=oracle,mssql. + """ + def decorator(func): + def wrapper(*args, **kwargs): + set_tags = [ + tag.strip() for tag in os.environ.get("TEST_TAGS", "").split(",") + if tag.strip() != "" + ] + if not tag in set_tags: + raise unittest.SkipTest("tag '{}' is not included in TEST_TAGS".format(tag)) + return func(*args, **kwargs) + wrapper.__name__ = func.__name__ + wrapper.__doc__ = func.__doc__ + return wrapper + + return decorator diff --git a/metricbeat/tests/system/metricbeat.py b/metricbeat/tests/system/metricbeat.py index ed21ecfe3e2..b0a0a232b0c 100644 --- a/metricbeat/tests/system/metricbeat.py +++ b/metricbeat/tests/system/metricbeat.py @@ -1,11 +1,12 @@ +import os import re import sys -import os import yaml sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../libbeat/tests/system'))) from beat.beat import TestCase +from beat.tags import tag from parameterized import parameterized_class COMMON_FIELDS = ["@timestamp", "agent", "metricset.name", "metricset.host",