0

I am working on banking project. While automating I am facing an issue. Below is the scenario.

In one of the test case say TC04 I will be getting customer_id which I will be storing it in a variable say customer_id_text. I need to use customer_id_text variable value for creating an account in TC05. But TC05 is not able to access this variable value. Could you please let me know how can I proceed further.

Ravi
  • 39
  • 5
  • Refactor the test code to be reusable across tests. Tests should ideally be able to be run independently of one another. You can create helper methods to place the code in question into the desirable state for the particular test. – flakes Sep 01 '20 at 05:31
  • This example explains how to access the varible outside and in another class. Check this https://stackoverflow.com/questions/19993795/how-would-i-access-variables-from-one-class-to-another – Siva Shanmugam Sep 01 '20 at 05:32
  • Does this answer your question? [Using global variables in a function](https://stackoverflow.com/questions/423379/using-global-variables-in-a-function) – rahul rai Sep 01 '20 at 05:43

1 Answers1

0

This can be done by adding global keyword in front of the variable name

Case I - Both class are present in the same File

class TC04(object):

    def customer_id_text(self, name, surname):
        global customer_id
        customer_id = name, surname
        return customer_id


t4 = TC04()
print(t4.customer_id_text('John', 'Wick'))


class TC05():

    def NameOfCutomer(self):
        return customer_id

tc05 = TC05()
print(tc05.NameOfCutomer())

Case II - Classes are present in the different Files - import FileName.ClassName

from StackOverflow.questions_63681807 import TC04

class TC05():

    def NameOfCutomer(self):
        return TC04.customer_id
Anmol Parida
  • 672
  • 5
  • 16