Unnati Shukla
Unnati Shukla

Reputation: 342

How to make values accessible across all test methods?

Generalized Scenario is as follows:

import unittest

class Test(unittest.TestCase):

    string1 = None
    def test1(self):
        self.string1 = "ValueAssignedFromMethod1"
        print "Test1 :"
        print self.string1

    def test2(self):
        print "\nTest 2 :" 
        print self.string1

if __name__ == "__main__":

        unittest.main()

Output of above Code is as follows:

Test1 :
ValueAssignedFromMethod1

Test 2 :
None

How can I use the Same "string1" variable across all methods & if the value gets changed in one method it should be available in other methods too?

In my Project I have following Scenario:[Using Python + Selenium Webdriver + Page Object Pattern + UnitTest Library]

class Test(unittest.TestCase):


    def redirectToFalconHostUI(self):
        #Start GOOGLE CHROME Browser
        self.browser = webdriver.Chrome(executable_path='E:\\chromedriver.exe')

        #Navigate to Website URL
        Site_Home = Home(self.browser)
        Site_Home.navigate()


    def testloginToWebsite(self):
        #Get Logged in the Falcon Web UI
        loginPage = Site_Home.getLoginForm()
        loginPage.enter_email(SINGLE_LOGIN_USERNAME)
        loginPage.enter_password(SINGLE_LOGIN_PASSWORD)
        Profile_Home = loginPage.get_logged_into_Site()

    def testProfilePageSection(self):
        Profile_home.go_to_Section1()

How can I get the current state of Browser's Webdriver in all next unittest method.

The page objects set in one method not available in next methods.

Upvotes: 0

Views: 400

Answers (2)

b4hand
b4hand

Reputation: 9770

Assign the variable in a setUp method. This will do the initialization before each test case is run.

import unittest

class Test(unittest.TestCase):

    def setUp(self):
        self.string1 = "ValueAssignedFromMethod1"

    def test1(self):
        print "Test1 :"
        print self.string1

    def test2(self):
        print "\nTest 2 :" 
        print self.string1

if __name__ == "__main__":
    unittest.main()

If you want the work to be done exactly once for the entire suite of tests, then you can use a setUpClass. You can assign the members for which you are interested to the class, and those will be accessible across methods.

Upvotes: 1

Unnati Shukla
Unnati Shukla

Reputation: 342

class Test(unittest.TestCase):
    Site_Home = None
    Profile_Home = None

    def redirectToFalconHostUI(self):
        #Start GOOGLE CHROME Browser
        self.browser = webdriver.Chrome(executable_path='E:\\chromedriver.exe')

        #Navigate to Website URL
        Test.Site_Home = Home(self.browser)
        Test.Site_Home.navigate()


    def testloginToWebsite(self):
        #Get Logged in the Falcon Web UI
        loginPage = Test.Site_Home.getLoginForm()
        loginPage.enter_email(SINGLE_LOGIN_USERNAME)
        loginPage.enter_password(SINGLE_LOGIN_PASSWORD)
        Test.Profile_Home = loginPage.get_logged_into_Site()

    def testProfilePageSection(self):
        Test.Profile_home.go_to_Section1()

By Having Class Level Objects it is accessible at all methods within the Same class. https://docs.python.org/2/tutorial/classes.html

Upvotes: 0

Related Questions