1

I'm very new to automation development, and currently starting to write an appium+pytest based Android app testing framework. I managed to run tests on a connected device using this code, that seems to use unittest:

class demo(unittest.TestCase):
    reportDirectory = 'reports'
    reportFormat = 'xml'
    dc = {}
    driver = None
    # testName = 'test_setup_tmotg_demo'
    
    def setUp(self):
        self.dc['reportDirectory'] = self.reportDirectory
        self.dc['reportFormat'] = self.reportFormat
        # self.dc['testName'] = self.testName
        self.dc['udid'] = 'RF8MA2GW1ZF'
        self.dc['appPackage'] = 'com.tg17.ud.internal'
        self.dc['appActivity'] = 'com.tg17.ud.ui.splash.SplashActivity'
        self.dc['platformName'] = 'android'
        self.dc['noReset'] = 'true'
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub',self.dc)

    # def test_function1():
    #   code

    # def test_function2():
    #   code

    # def test_function3():
    #   code
    # etc...

    def tearDown(self):
        self.driver.quit()
        
if __name__ == '__main__':
    unittest.main()

As you can see all the functions are currently within 'demo' class. The intention is to create several test cases for each part of the app (for example: registration, main screen, premium subscription, etc.). That could sum up to hundreds of test cases eventually. It seems to me that simply continuing listing them all in this same class would be messy and would give me a very limited control. However I didn't find any other way to arrange my tests while keeping the device connected via appium.

The question is what would be the right way to organize the project so that I can:

  1. Set up the device with appium server
  2. Run all the test suites in sequential order (registration, main screen, subscription, etc...).
  3. Perform the cleaning... export results, disconnect device, etc.

I hope I described the issue clearly enough. Would be happy to elaborate if needed.

Shen
  • 11
  • 2

1 Answers1

0

Well you have a lot of questions here so it might be good to split them up into separate threads. But first of all you can learn a lot about how Appium works by checking out the documentation here. And for the unittest framework here.

  1. All Appium cares about is the capabilities file (or variable). So you can either populate it manually or white some helper function to do that for you. Here is a list of what can be used.
  2. You can create as many test classes(or suites) as you want and add them together in any order you wish. This helps to break things up into manageable chunks. (See example below)
  3. You will have to create some helper methods here as well, since Appium itself will not do much cleaning. You can use the adb command in the shell for managing android devices.
import unittest
from unittest import TestCase


# Create a Base class for common methods
class BaseTest(unittest.TestCase):

    # setUpClass method will only be ran once, and not every suite/test
    @classmethod
    def setUpClass(cls) -> None:
        # Init your driver and read the capabilites here
        pass

    @classmethod
    def tearDownClass(cls) -> None:
        # Do cleanup, close the driver, ...
        pass


# Use the BaseTest class from before
# You can then duplicate this class for other suites of tests
class TestLogin(BaseTest):
    
    @classmethod
    def setUpClass(cls) -> None:
        super(TestLogin, cls).setUpClass()
        # Do things here that are needed only once (like loging in)

    def setUp(self) -> None:
        # This is executed before every test
        pass

    def testOne(self):
        # Write your tests here
        pass
    
    def testTwo(self):
        # Write your tests here
        pass

    def tearDown(self) -> None:
        # This is executed after every test
        pass


if __name__ == '__main__':
    # Load the tests from the suite class we created
    test_cases = unittest.defaultTestLoader.loadTestsFromTestCase(TestLogin)
    # If you want do add more
    test_cases.addTests(TestSomethingElse)

    # Run the actual tests
    unittest.TextTestRunner().run(test_cases)

JostB
  • 126
  • 4