python - How to make values accessible across all test methods? -


generalized scenario 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 follows:

test1 : valueassignedfrommethod1  test 2 : none 

how can use same "string1" variable across methods & if value gets changed in 1 method should available in other methods too?

in project 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 website url         site_home = home(self.browser)         site_home.navigate()       def testlogintowebsite(self):         #get logged in 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 current state of browser's webdriver in next unittest method.

the page objects set in 1 method not available in next methods.

assign variable in setup method. initialization before each test case 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 want work done once entire suite of tests, can use setupclass. can assign members interested class, , accessible across methods.


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -