Package tlib :: Package base :: Module SpiraModerator |
[hide private]
[frames] | no frames]

# Source Code for Module tlib.base.SpiraModerator

      1  from logging import Logger
      2  from suds.xsd.doctor import Import, ImportDoctor
      3  import suds
      4  from SoapRequestor import SoapRequestor
      5  from warnings import warn
      6  from html2text import HTML2Text
      7  from tlib.conftest import prj_config
      8
      9
     10 -class SpiraModerator(object):
     11      """
     12      Helper class to connect and manipulate the data in SpiraTest
     13      """
     14
     15      client = None                        #: Soap client to connect to Spira Soap API
     16      my_soap = None
     17      _tests = None                        #: Holds retrieved tests from Automation test set in Spiratest
     18      _logger = None                #: logger to send loggin information to.  Logger comes from pytest test definitions
     19
     20      _username = prj_config.get("spira", "username")
     21      _password = prj_config.get("spira", "password")
     22
     23 -    def __init__(self, logger):
     24          """
     25          Constructor for class
     26
     27          @param logger: instance of a logging object configured in testing project
     28          @type logger: Logger
     29          """
     30          self._logger = logger
     31          wsdl_file = "http://spirateam.ypg.com/Services/v4_0/ImportExport.svc?wsdl"
     32          schema_url = 'http://schemas.microsoft.com/2003/10/Serialization/Arrays'
     33          schema_import = Import(schema_url)
     34          schema_doctor = ImportDoctor(schema_import)
     35
     36          self.client = SoapRequestor(self._logger, wsdl_file, schema_doctor).client
     37
     38 -    def test_connect(self):
     39          """
     40          Authenticates client with SpiraTest.
     41
     42          @return: if connection was successful
     43          @rtype: bool
     44          """
     45          warn("This method is Deprecated; you may want to update it use connect() instead.", DeprecationWarning)
     46          self.connect()
     47
     48 -    def connect(self):
     49          """
     50          Authenticates client with SpiraTest.
     51
     52          @return:if connection was successful
     53          @rtype: bool
     54          """
     55
     56          result = False
     57          try:
     58              result = self.client.service.Connection_Authenticate(self._username, self._password)
     59          except suds.WebFault, e:
     60              self._logger.error(str(e))
     61
     62          self._logger.info("The connection to Spira returned :" + str(result))
     63
     64          return result
     65
     66 -    def update_test_case(self, project_id, test_case_id, test_start, test_end, test_status_id, runner_msg,
     67                           stack_trace, test_set_id):
     68          """
     69          Updates a specified test cases with a specified projects Automation test set with the results of the test execution
     70
     71          @param project_id: Unique project id from within SpiraTest
     72          @type project_id: int
     73          @param test_case_id: Unique test case id from within SpiraTest
     74          @type test_case_id: int
     75          @param test_start: Time at which the test case started its execution. Doesn't seem to affect Spiratest.
     76          @type test_start: datetime
     77          @param test_end: Time at which the test case ended its execution. Doesn't seem to affect Spiratest.
     78          @type test_end: datetime
     79          @param runner_msg: Parameters that were used in test execution
     80          @type runner_msg: str
     81          @param stack_trace: Error message returned from failed test cases
     82          @type stack_trace: str
     83
     84          """
     85
     86          self.client.service.TestRun_RecordAutomated2(self._username, self._password, project_id, None,
     87                                                               test_case_id, None, test_set_id, None, None, test_start,
     88                                                               test_end, test_status_id, "None", None, 1,
     89                                                               runner_msg, stack_trace, 1)
     90
     91 -    def get_project_id(self, proj_name):
     92          """
     93          Gets the Unique prject id from spiratest for a given project name.  Name comes from project's config file
     94
     95          @param proj_name: Name of project under test
     96          @type proj_name: str
     97
     98          @return: project id
     99          @rtype: int
    100          """
    101
    102          self.client.service.Connection_Authenticate(self._username, self._password)
    103          project_list = self.client.service.Project_Retrieve()
    104
    105          project = (proj for proj in project_list[0] if proj["Name"] == proj_name).next()
    106          self._logger.info("Project Id returned is " + str(project.ProjectId))
    107
    108          return project.ProjectId
    109
    110 -    def get_tests_from_set(self, test_set_id, proj_id):
    111          """
    112          Fetches a list of test cases for a given test set id in SpiraTest and stores them in class var "tests"
    113
    114          @param test_set_id: Automation test set id from Spiratest for project under test
    115          @type test_set_id: int
    116          @param proj_id: project id from Spiratest for project under test
    117          @type proj_id: int
    118          """
    119
    120          self.client.service.Connection_Authenticate(self._username, self._password)
    121          self.client.service.Connection_ConnectToProject(proj_id)
    122          test_set = self.client.service.TestCase_RetrieveByTestSetId(test_set_id)
    123          self._tests = test_set[0]
    124
    125 -    def get_test_case_id(self, test_name):
    126          """
    127          Gets the unique test case id from SpiraTest for the test case name supplied
    128
    129          @param test_name: Test case name found in SpiraTest and used in test case decorated to label
    130          automated test_support
    131          @type test_name: str
    132
    133          @return: test case id
    134          @rtype: int
    135          """
    136
    137          my_test = (test for test in self._tests if test["Name"] == test_name).next()
    138
    139          return my_test.TestCaseId
    140
    141 -    def get_test_steps(self,test_case_id):
    142          """
    143          Gets the Test Steps for a Test Case
    144
    145          @param test_case_id: Test Case ID from the SpiraTest
    146          @type test_case_id: int
    147
    148          @return: test steps including Test Description. Expected Results and Test Data
    149          @rtype: list
    150          """
    151          arr_test_steps = []
    152
    153          test_steps = self.client.service.TestCase_RetrieveById(test_case_id)
    154
    155          #Calculates index of the last step in ArrayOfRemoteTestStep.Array of RemoteTestStep
    156          last_test_step = len(test_steps.TestSteps[0])
    157
    158          for i in range(last_test_step):
    159              arr_test_steps.extend([test_steps.TestSteps[0][i].Description, test_steps.TestSteps[0][i].ExpectedResult,
    160                         test_steps.TestSteps[0][i].SampleData])
    161
    162          return arr_test_steps
    163
    164 -    def get_expected_result(self, test_case_id):
    165          """
    166          Gets the Expected Result of the last Test Step for a test case
    167
    168          @param test_case_id: Test Case ID from the SpiraTest
    169          @type test_case_id: int
    170
    171          @return: expected test step result in plain text
    172          @rtype: str
    173          """
    174
    175          test_steps = self.get_test_steps(test_case_id)
    176
    177          expected_result_of_last_step = test_steps[-2]
    178
    179          # Converts Expected Result in HTML form to plain text
    180          handler = HTML2Text()
    181          expected_result = handler.handle(expected_result_of_last_step)
    182
    183          return expected_result
    184
    185 -    def get_test_by_id(self, test_case_id, proj_id):
    186          """
    187          Gets the test info in SpiraTest for a specific test case and stores them in class var "tests"
    188
    189          @param test_case_id: Test case id from Spiratest for a specific Test Case in the Project under test
    190          @type test_case_id: int
    191          @param proj_id: project id from Spiratest for project under test
    192          @type proj_id: int
    193          """
    194
    195          self.client.service.Connection_Authenticate(self._username, self._password)
    196          self.client.service.Connection_ConnectToProject(proj_id)
    197          self._tests = self.client.service.TestCase_RetrieveById(test_case_id)
    198
    199
    200 -    def get_tests_in_folder(self, folder_id, proj_id):
    201          """
    202          Fetches a list of test cases available in a specific folder in SpiraTest and stores them in class var "tests"
    203
    204          @param folder_id: Folder in Spiratest containg a list of Test Cases for the project under test
    205          @type folder_id: int
    206          @param proj_id: project id from Spiratest for project under test
    207          @type proj_id: int
    208          """
    209
    210          self.client.service.Connection_Authenticate(self._username, self._password)
    211          self.client.service.Connection_ConnectToProject(proj_id)
    212          tests = self.client.service.TestCase_RetrieveByFolder(folder_id)
    213          self._tests = tests[0]
    214
    215 -    def connect_to_project(self, proj_id):
    216          """
    217          Connects to a given project in spira
    218          @param proj_id: (int) unique id of the project in spira (you can see it in the address bar in Spira
    219          @return: (bool) connection status
    220          """
    221          return self.client.service.Connection_ConnectToProject(proj_id)
    222
    223 -    def get_test(self, test_name):
    224          """
    225          Fetches a test by the test's name.  You must connect to Spira first as well as connect to a project
    226          @param test_name: (str) name of the test case
    227          @return: (list) Spira test case object(s) with the same name as entered.
    228          """
    229          array_of_filters = self.client.factory.create('ns1:ArrayOfRemoteFilter')
    230          a_filter = self.client.factory.create('ns1:RemoteFilter')
    231          a_filter.PropertyName = 'Name'
    232          a_filter.StringValue = test_name
    233          array_of_filters.RemoteFilter.append(a_filter)
    234
    235          tests = self.client.service.TestCase_Retrieve(array_of_filters, 1, 999999)
    236
    237          to_return = []
    238          for test in tests.RemoteTestCase:
    239              if not test.Folder:
    240                  to_return.append(test)
    241
    242          return to_return
    243