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

# Source Code for Module tlib.base.SpiraModerator

      1  from suds.xsd.doctor import Import, ImportDoctor
      2  import suds
      3  from SoapRequestor import SoapRequestor
      4  from warnings import warn
      5  from html2text import HTML2Text
      6  from tlib.conftest import prj_config
      7
      8
      9 -class SpiraModerator(object):
     10      """
     11      Helper class to connect and manipulate the data in SpiraTest
     12      """
     13
     14      client = None                        #: Soap client to connect to Spira Soap API
     15      my_soap = None
     16      _tests = None                        #: Holds retrieved tests from Automation test set in Spiratest
     17      _logger = None                #: logger to send loggin information to.  Logger comes from pytest test definitions
     18
     19      _username = prj_config.get("spira", "username")
     20      _password = prj_config.get("spira", "password")
     21
     22 -    def __init__(self, _logger):
     23          """
     24          Constructor for class
     25
     26          Args :
     27          _logger : (logger) instance of a logging object configured in testing project
     28
     29          """
     30
     31          self._logger = _logger
     32          wsdl_file = "http://mtljiraqcprod01.ad.ypg.com/SpiraTeam/Services/v4_0/ImportExport.svc?wsdl"
     33          schema_url = 'http://schemas.microsoft.com/2003/10/Serialization/Arrays'
     34          schema_import = Import(schema_url)
     35          schema_doctor = ImportDoctor(schema_import)
     36
     37          self.my_soap = SoapRequestor(self._logger, wsdl_file, schema_doctor)
     38
     39 -    def test_connect(self):
     40          """
     41          Authenticates client with SpiraTest.
     42
     43          Returns : (bool) if connection was successful
     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          Returns : (bool) if connection was successful
     53          """
     54
     55          result = False
     56          try:
     57              result = self.my_soap.client.service.Connection_Authenticate(self._username, self._password)
     58          except suds.WebFault, e:
     59              self._logger.error(str(e))
     60
     61          self._logger.info("The connection to Spira returned :" + str(result))
     62
     63          return result
     64
     65 -    def update_test_case(self, project_id, test_case_id, test_start, test_end, test_status_id, runner_msg,
     66                           stack_trace, test_set_id):
     67          """
     68          Updates a specified test cases with a specified projects Automation test set with the results of the test execution
     69
     70          Args :
     71          project_id : (int) Unique project id from within SpiraTest
     72          test_case_id : (int) Unique test case id from within SpiraTest
     73          test_start : (datetime) Time at which the test case started its execution. Doesn't seem to affect Spiratest.
     74          test_end : (datetime) Time at which the test case ended its execution. Doesn't seem to affect Spiratest.
     75          runner_msg : (str) Parameters that were used in test execution
     76          stack_trace : (str) Error message returned from failed test cases
     77
     78          """
     79
     80          self.my_soap.client.service.TestRun_RecordAutomated2(self._username, self._password, project_id, None,
     81                                                               test_case_id, None, test_set_id, None, None, test_start,
     82                                                               test_end, test_status_id, "None", None, 1,
     83                                                               runner_msg, stack_trace, 1)
     84
     85 -    def get_project_id(self, proj_name):
     86          """
     87          Gets the Unique prject id from spiratest for a given project name.  Name comes from project's config file
     88
     89          Args : proj_name : (str) Name of project under test
     90
     91          Returns : (int) project id
     92          """
     93
     94          self.my_soap.client.service.Connection_Authenticate(self._username, self._password)
     95          project_list = self.my_soap.client.service.Project_Retrieve()
     96
     97          project = (proj for proj in project_list[0] if proj["Name"] == proj_name).next()
     98          self.logger.info("Project Id returned is " + str(project.ProjectId))
     99
    100          return project.ProjectId
    101
    102 -    def get_tests_from_set(self, test_set_id, proj_id):
    103          """
    104          Fetches a list of test cases for a given test set id in SpiraTest and stores them in class var "tests"
    105
    106          Args :
    107          test_set_id : (int) Automation test set id from Spiratest for project under test
    108          proj_id : (int) project id from Spiratest for project under test
    109
    110          """
    111
    112          self.my_soap.client.service.Connection_Authenticate(self._username, self._password)
    113          self.my_soap.client.service.Connection_ConnectToProject(proj_id)
    114          test_set = self.my_soap.client.service.TestCase_RetrieveByTestSetId(test_set_id)
    115          self._tests = test_set[0]
    116
    117 -    def get_test_case_id(self, test_name):
    118          """
    119          Gets the unique test case id from SpiraTest for the test case name supplied
    120
    121          Args : testName : (str) Test case name found in SpiraTest and used in test case decorated to label
    122                              automated test_support
    123
    124          Returns : (int) test case id
    125          """
    126
    127          my_test = (test for test in self._tests if test["Name"] == test_name).next()
    128
    129          return my_test.TestCaseId
    130
    131 -    def get_test_steps(self,test_case_id):
    132          """
    133          Gets the Test Steps for a Test Case
    134
    135          Args : test_case_id : (int) Test Case ID from the SpiraTest
    136
    137          Returns : (array) test steps including Test Description. Expected Results and Test Data
    138          """
    139          arr_test_steps = []
    140
    141          test_steps = self.my_soap.client.service.TestCase_RetrieveById(test_case_id)
    142
    143          #Calculates index of the last step in ArrayOfRemoteTestStep.Array of RemoteTestStep
    144          last_test_step = len(test_steps.TestSteps[0])
    145
    146          for i in range(last_test_step):
    147              arr_test_steps.extend([test_steps.TestSteps[0][i].Description, test_steps.TestSteps[0][i].ExpectedResult,
    148                         test_steps.TestSteps[0][i].SampleData])
    149
    150          return arr_test_steps
    151
    152 -    def get_expected_result(self,test_case_id):
    153          """
    154          Gets the Expected Result of the last Test Step for a test case
    155
    156          Args : test_case_id : (int) Test Case ID from the SpiraTest
    157
    158          Returns : (str) expected test step result in plain text
    159          """
    160
    161          test_steps = self.get_test_steps(test_case_id)
    162
    163          expected_result_of_last_step = test_steps[-2]
    164
    165          # Converts Expected Result in HTML form to plain text
    166          handler = HTML2Text()
    167          expected_result = handler.handle(expected_result_of_last_step)
    168
    169          return expected_result
    170