#!/usr/bin/env python # Copyright (c) 2016 Orange and others. # # All rights reserved. This program and the accompanying materials # are made available under the terms of the Apache License, Version 2.0 # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 """Define the parent class of all Functest TestCases.""" import logging import os import functest.utils.functest_utils as ft_utils __author__ = "Cedric Ollivier <cedric.ollivier@orange.com>" class TestCase(object): """Base model for single test case.""" EX_OK = os.EX_OK """everything is OK""" EX_RUN_ERROR = os.EX_SOFTWARE """run() failed""" EX_PUSH_TO_DB_ERROR = os.EX_SOFTWARE - 1 """push_to_db() failed""" EX_TESTCASE_FAILED = os.EX_SOFTWARE - 2 """results are false""" __logger = logging.getLogger(__name__) def __init__(self, **kwargs): self.details = {} self.project_name = kwargs.get('project_name', 'functest') self.case_name = kwargs.get('case_name', '') self.criteria = kwargs.get('criteria', 100) self.result = 0 self.start_time = 0 self.stop_time = 0 def __str__(self): try: assert self.project_name assert self.case_name result = 'PASS' if(self.is_successful( ) == TestCase.EX_OK) else 'FAIL' return ('| {0:<23} | {1:<13} | {2:<10} | {3:<13} |' '\n{4:-<26}{4:-<16}{4:-<13}{4:-<16}{4}'.format( self.case_name, self.project_name, self.get_duration(), result, '+')) except AssertionError: self.__logger.error("We cannot print invalid objects") return '| {0:^68} |\n{1:-<26}{1:-<16}{1:-<13}{1:-<16}{1}'.format( 'INVALID OBJECT', '+') def get_duration(self): """Return the duration of the test case. Returns: duration if start_time and stop_time are set "XX:XX" otherwise. """ try: assert self.start_time assert self.stop_time if self.stop_time < self.start_time: return "XX:XX" return "{0[0]:02.0f}:{0[1]:02.0f}".format(divmod( self.stop_time - self.start_time, 60)) except Exception: # pylint: disable=broad-except self.__logger.error("Please run test before getting the duration") return "XX:XX" def is_successful(self): """Interpret the result of the test case. It allows getting the result of TestCase. It completes run() which only returns the execution status. It can be overriden if checking result is not suitable. Returns: TestCase.EX_OK if result is 'PASS'. TestCase.EX_TESTCASE_FAILED otherwise. """ try: assert self.criteria if (not isinstance(self.result, str) and not isinstance(self.criteria, str)): if self.result >= self.criteria: return TestCase.EX_OK else: # Backward compatibility # It must be removed as soon as TestCase subclasses # stop setting result = 'PASS' or 'FAIL'. # In this case criteria is unread. self.__logger.warning( "Please update result which must be an int!") if self.result == 'PASS': return TestCase.EX_OK except AssertionError: self.__logger.error("Please run test before checking the results") return TestCase.EX_TESTCASE_FAILED def run(self, **kwargs): """Run the test case. It allows running TestCase and getting its execution status. The subclasses must override the default implement<style>.highlight .hll { background-color: #ffffcc } .highlight .c { color: #888888 } /* Comment */ .highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */ .highlight .k { color: #008800; font-weight: bold } /* Keyword */ .highlight .ch { color: #888888 } /* Comment.Hashbang */ .highlight .cm { color: #888888 } /* Comment.Multiline */ .highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */ .highlight .cpf { color: #888888 } /* Comment.PreprocFile */ .highlight .c1 { color: #888888 } /* Comment.Single */ .highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */ .highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Built