Package: pytest-fluent
pytest-fluentpytest-fluentis a Python package in order to extendpytestusingFluentdfor logging test results.Descriptionpytestis one of the most powerful testing suites with a lot of functionality and many plugins.Fluentdis a well-established log collector which is used in many modern web architectures. So, why not putting both worlds together in order to gain real-time log access to your distributed test runs?pytest-fluentenables you to forward your test data immediately to your preferred log-sink from any node spread around your infrastructure. The streamed data are available for each pytest stage and formatted in JSON.Eachpytestsession gets an unique identifier (UID) assigned as well as each executed test case. With these UIDs, you can easily filter sessions and testcase data entries, for instance in your favorite database.InstallationThe package is available atpypi.organd can be installed by typingpipinstallpytest-fluentUsagepytest-fluent-loggingforwards meta data frompytesttoFluentdfor further processing. The meta data areunique session IDunique test IDstatus of the session respectively test casetest case nametest resultsrecord_propertyentriescustom testcase informationcustom session informationtimestampsFurthermore, the Python logging instance can be extended in order to forward test case runtime logging:fromloggingimportgetLoggerdeftest_my_runtime_log():value=1getLogger().info("Setting value to%s",value)assertvalue==1or:fromloggingimportgetLoggerdeftest_my_runtime_log():value=1getLogger('fluent').info("Setting value to%s",value)assertvalue==1FixturesIn order to create your own logger, request the fixtureget_loggeras follows:deftest_my_runtime_log(get_logger):logger=get_logger('my.Logger')value=1logger.info("Setting value to%s",value)assertvalue==1If you want to get the current UIDs, use thesession_uidandtest_uidfixtures as follows:deftest_unique_identifier(get_logger,session_uid,test_uid):logger=get_logger('fluent')logger.info("Session ID:%s",session_uid)logger.info("Test ID:%s",test_uid)value=1assertvalue==1CallbacksIf you want to add custom data to the datasets of thepytest_sessionstartandpytest_runtest_logstartstages, decorate your callback functions with the following decorators:frompytest_fluentimport(additional_session_information_callback,additional_test_information_callback)@additional_session_information_callbackdefprovide_more_session_information()->dict:return{"more":"session information"}@additional_test_information_callbackdefprovide_more_test_information()->dict:return{"more":"test information"}pytestCLI extensionsThepytestCLI application can be called with the following arguments in order to configurefluent-logging.argumentdescriptiondefault--session-uuidUse a custom externally created UUID, e.g. link a CI job with the pytest session.--fluentd-hostFluentd host address. If not provided, a local Fluentd instance will be called.--fluentd-portFluent host port24224--fluentd-tagSet a custom Fluentd tag'test'--fluentd-labelSet a custom Fluentd label'pytest'--fluentd-timestampSpecify a Fluentd timestampNone--extend-loggingExtend the Python logging with a Fluent handlerFalse--add-docstringsAdd test docstrings to testcase call messages--stage-settingsUse custom stage settings file. SeedocumentationIni Configuration SupportDefault values of the CLI arguments for a project could also be defined in one of the following ini configuration files:pytest.ini: Arguments are defined under thepytestsection in the file. This file takes precedence over all other configuration files even if it's empty.[pytest]addopts=--session-uuid="ac2f7600-a079-46cf-a7e0-6408b166364c"--fluentd-port=24224--fluentd-host=localhost--fluentd-tag='dummytest'--fluentd-label='pytest'--extend-loggingpyproject.toml: are considered for configuration when they contain atool.pytest.ini_optionssection is available[tool.pytest.ini_options]addopts="--fluentd-port=24224 --fluentd-host=localhost --fluentd-tag='test' --fluentd-label='pytest' --extend-logging"tox.ini: can also be used to hold pytest configuration if they have a [pytest] section.[pytest]addopts=--fluentd-port=24224--fluentd-host=localhost--fluentd-tag='test'--fluentd-label='pytest'If the same option is specified in both CLI andinifile, then CLI option would have higher priority and override theinifile values.What data are sent?pytest-fluentsends any information, e.g. stage information or logging from a test case, as a single chunk. For instance, the data collection fromtest_addoptions.pytest looks as following[{"status":"start","stage":"session","sessionId":"d8f01de3-8416-4801-9406-0ea3d5cfe3c0"},{"status":"start","stage":"testcase","sessionId":"d8f01de3-8416-4801-9406-0ea3d5cfe3c0","testId":"6b444275-4450-4eff-b5d9-8355f0f99ab0","name":"test_fluentd_logged_parameters.py::test_base"},{"type":"logging","host":"myComputer","where":"test_fluentd_logged_parameters.test_base","level":"INFO","stack_trace":"None","message":"Logged from test_base","sessionId":"d8f01de3-8416-4801-9406-0ea3d5cfe3c0","testId":"6b444275-4450-4eff-b5d9-8355f0f99ab0","stage":"testcase"},{"type":"logging","host":"myComputer","where":"test_fluentd_logged_parameters.test_base","level":"INFO","stack_trace":"None","message":"Logged from test_base","sessionId":"d8f01de3-8416-4801-9406-0ea3d5cfe3c0","testId":"6b444275-4450-4eff-b5d9-8355f0f99ab0","stage":"testcase"},{"name":"test_fluentd_logged_parameters.py::test_base","outcome":"passed","duration":0.0013457999999999526,"markers":{"test_base":1,"test_fluentd_logged_parameters.py":1,"test_fluentd_logged_parameters0":1},"stage":"testcase","when":"call","sessionId":"d8f01de3-8416-4801-9406-0ea3d5cfe3c0","testId":"6b444275-4450-4eff-b5d9-8355f0f99ab0"},{"status":"finish","stage":"testcase","sessionId":"d8f01de3-8416-4801-9406-0ea3d5cfe3c0","testId":"6b444275-4450-4eff-b5d9-8355f0f99ab0","name":"test_fluentd_logged_parameters.py::test_base"},{"status":"finish","duration":0.00297708511352539,"stage":"session","sessionId":"d8f01de3-8416-4801-9406-0ea3d5cfe3c0"}]whereat each object in the array is sent independently viaFluentd.Specifying a timestampTimestamps are added to the information if the--fluentd-timestampoption is enabled:[pytest]addopts=--session-uuid="ac2f7600-a079-46cf-a7e0-6408b166364c"--fluentd-port=24224--fluentd-host=localhost--fluentd-tag='dummytest'--fluentd-label='pytest'--fluentd-timestamp='@timestamp'--extend-loggingThe timestamp is added to each message. The value is in ISO 8601 format. A sample
of the data collection fromtest_addoptions.py(as above) would look as below:[{"status":"start","stage":"session","sessionId":"d8f01de3-8416-4801-9406-0ea3d5cfe3c0","@timestamp":"2022-12-25T03:00:00.000000Z"},{"status":"start","stage":"testcase","sessionId":"d8f01de3-8416-4801-9406-0ea3d5cfe3c0","testId":"6b444275-4450-4eff-b5d9-8355f0f99ab0","name":"test_fluentd_logged_parameters.py::test_base","@timestamp":"2022-12-25T03:00:00.100000Z"}]ChangelogThe changelog.ContributingWe welcome any contributions, enhancements, and bug-fixes. Open anissueonGithubandsubmit a pull request.