Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 1 | # Copyright 2016 Mirantis, Inc. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
| 15 | from __future__ import division |
| 16 | import time |
| 17 | |
| 18 | import pytest |
| 19 | |
| 20 | from tcp_tests import logger |
| 21 | from tcp_tests.helpers import log_step |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 22 | |
| 23 | |
| 24 | LOG = logger.logger |
| 25 | |
| 26 | |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 27 | @pytest.hookimpl(tryfirst=True, hookwrapper=True) |
| 28 | def pytest_runtest_makereport(item, call): |
| 29 | outcome = yield |
| 30 | rep = outcome.get_result() |
| 31 | setattr(item, "rep_" + rep.when, rep) |
| 32 | |
| 33 | |
| 34 | def pytest_runtest_setup(item): |
| 35 | if item.cls is not None: |
| 36 | item.cls._current_test = item.function |
| 37 | item._start_time = time.time() |
| 38 | head = "<" * 5 + "#" * 30 + "[ {} ]" + "#" * 30 + ">" * 5 |
| 39 | head = head.format(item.function.__name__) |
| 40 | start_step = "\n{head}".format(head=head) |
| 41 | LOG.info(start_step) |
| 42 | |
| 43 | |
| 44 | def pytest_runtest_teardown(item): |
| 45 | step_name = item.function.__name__ |
| 46 | if hasattr(item, '_start_time'): |
| 47 | spent_time = time.time() - item._start_time |
| 48 | else: |
| 49 | spent_time = 0 |
| 50 | minutes = spent_time // 60 |
| 51 | seconds = int(round(spent_time)) % 60 |
| 52 | finish_step = "FINISH {} TEST. TOOK {} min {} sec".format( |
| 53 | step_name, minutes, seconds |
| 54 | ) |
Dennis Dmitriev | 2d643bc | 2017-12-04 12:23:47 +0200 | [diff] [blame] | 55 | print("\n\n") |
Dennis Dmitriev | 6f59add | 2016-10-18 13:45:27 +0300 | [diff] [blame] | 56 | foot = "\n" + "<" * 5 + "#" * 30 + "[ {} ]" + "#" * 30 + ">" * 5 |
| 57 | foot = foot.format(finish_step) |
| 58 | LOG.info(foot) |
| 59 | |
| 60 | |
| 61 | @pytest.fixture(scope='function') |
| 62 | def show_step(request): |
| 63 | def _show_step(step_number): |
| 64 | return log_step.log_step(request.function, step_number) |
| 65 | return _show_step |
Dennis Dmitriev | 474e3f7 | 2016-10-21 16:46:09 +0300 | [diff] [blame] | 66 | |
Dina Belova | e6fdffb | 2017-09-19 13:58:34 -0700 | [diff] [blame] | 67 | |
Dennis Dmitriev | 474e3f7 | 2016-10-21 16:46:09 +0300 | [diff] [blame] | 68 | @pytest.fixture(scope='function') |
| 69 | def steps(request): |
| 70 | steps_mark = request.keywords.get('steps', None) |
| 71 | steps = steps_mark.args[0] |
| 72 | return steps |
Dennis Dmitriev | 2d643bc | 2017-12-04 12:23:47 +0200 | [diff] [blame] | 73 | |
| 74 | |
| 75 | @pytest.fixture(scope='function', autouse=True) |
| 76 | def func_name(request): |
| 77 | """Name of the current test function""" |
| 78 | return getattr(request.node.function, '_name', |
| 79 | request.node.function.__name__) |