Python. SetupClass? Complex automation

Hi,

I’m trying to run through a complete application and do task, after task, after task within the same session.

I notice that setUp and tearDown run on each function. How can I have only 1 inital Setup and then a final tearDown at the end?

Here is my code:

Thanks

    '''
Created on Nov 17, 2014

@author: Paul
'''
import time
import os
import unittest
from appium import webdriver

class Test(unittest.TestCase):
    def setUp(self):
        "Setup for the test"
        desired_caps = {}
        desired_caps['platformName'] = 'Android'
        desired_caps['platformVersion'] = '4.4'
        desired_caps['deviceName'] = 'Android Emulator'
        # Returns abs path relative to this file and not cwd
        desired_caps['app'] = os.path.abspath(os.path.join(os.path.dirname(__file__),'com.tinder-4.0.1-APK4Fun.com.apk'))
        desired_caps['appPackage'] = 'com.tinder'
        desired_caps['appActivity'] = 'com.tinder.activities.ActivitySplashLoading'
        desired_caps['appium-version'] = '1.1'
        self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)

    def tearDown(self):
        "Tear down the test"
        self.driver.quit()

    def test_login(self):
        time.sleep(10)
        el = self.driver.find_element_by_xpath('//android.widget.Button[contains(@text, "Sign in with Facebook")]')
        el.click()
        
    def test_credentials(self):
        time.sleep(40)
            
        textfields = self.driver.find_elements_by_class_name("//android.widget.EditText")
            
        print(textfields)
             
        #textfields[4].send_keys("Appium User")
        #textfields[3].send_keys("[email protected]")
        
#---START OF SCRIPT
if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(Test)
    unittest.TextTestRunner(verbosity=2).run(suite)
1 Like

This isn’t Appium related. You should ask a question on StackOverflow in the unittest, python tags.

Well.

I usually use the nose module (link) which is a lot more flexible when doing things like setup and teardown, so I didn’t know off the top of my head.

Regardless, here’s a working example of a setup function being called once per test case class

import unittest

class Test(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        print "Setup the class!"

    @classmethod
    def tearDownClass(cls):
        print "Tear down the class!"
    
    def test_login(self):
        print "I just tested login. It looked great"

    def test_credentials(self):
        print "I just tested credentials."


#---START OF SCRIPT
if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(Test)
    unittest.TextTestRunner(verbosity=1).run(suite)

Thanks for the assist. I do appreciate it!

I actually found similar code here: http://stackoverflow.com/questions/24310245/appium-ui-navigation-issue-python-script

Although it didnt work as I expected. I obviously got something wrong somewhere.

Honestly, I find the whole automation on this a kludge. Eclipse, nodejs, appium, android emulator, windows, java, so many disparate tech trying to work together and on a virtual machine to boot :confused:

Thanks

U can use @classmethod on top of setup and teardown.

hi ,
may i ask for help to configure appium and make it works with python?