Appium python unittest pageobject How to implement loading multiple case

Source: Internet
Author: User
Tags appium

Learn the Appium Python project the small partner will have a question, saying that now all the case is a way to add a method through a suite, but in the actual process we do not want this, we do the function is this:

Suite = UnitTest. TestSuite () suite.addtest (Casetest ("test_02", parame=i)) unittest. Texttestrunner (). Run (Suite)

This is what we do now, but what we want is:

Discover = Unittest.defaultTestLoader.discover (case_dir,pattern= ' test_*.py ') unittest. Texttestrunner (). Run (Discover)

So how do we get enough of what we do to replace what we want? I'm here to tell you the answer, you're not yet!

Why do you say that? First we need to know a premise, what is the discover we get now? What is our operating mechanism? Let's look at the following two simple code:

#coding =utf-8import unittestclass studytest (unittest. TestCase):    def test_study01 (self):        print ("Studytest de.")    def test_study02 (self):        print (" Studytest de 02 ")

  

#coding =utf-8import unittestclass StudyTest01 (unittest. TestCase):    def test_study001 (self):        print ("studytest01 de 001")    def test_study002 (self):        print (" Studytest01 de 002 ") if __name__ = = ' __main__ ':    test_dit = './'    Discovre = Unittest.defaultTestLoader.discover ( test_dit,pattern= ' study_*.py ')    print (discovre)    runner = UnitTest. Texttestrunner ()    Runner.run (Discovre)

The above two code we simply implemented the implementation of the Discover, here is not what we want is this? But why can't we say no? At least you can't do it now, first look at what we're printing discover.

<unittest.suite.testsuite tests=[
< Unittest.suite.TestSuite tests=[]>,
<unittest.suite.testsuite tests=[]>,
< Unittest.suite.TestSuite tests=[]>,
<unittest.suite.testsuite tests=[
<unittest.suite.testsuite Tests=[]>,
<unittest.suite.testsuite tests=[<case.study_test. Studytest Testmethod=test_study01>, <case.study_test. Studytest testmethod=test_study02>]>]>,
<unittest.suite.testsuite tests=[
< Unittest.suite.TestSuite tests=[<case.study_test01. StudyTest01 Testmethod=test_study001>, <case.study_test01. StudyTest01 testmethod=test_study002>]>]>,
<unittest.suite.testsuite tests=[]>,
< Unittest.suite.TestSuite tests=[]>,
<unittest.suite.testsuite tests=[]>,
< Unittest.suite.TestSuite Tests=[]>]>

Through the above print I'm sure you can see that what we call a discover is actually a collection of all of our cases (it's inappropriate here, but easier to understand), and this is our use case, and this time I just have to go to the run. So the question is, how do we need to pass the parameters? Did you find that way we couldn't pass the parameters? Then the small partners found a way through the course of the course, and we talked about the methods

A global variable, but this we need a way to add a method, which is not our purpose, so we can find the method by looking at the net, look at the code:

#-*-Coding:utf-8-*-

#author: Mushishi

#Date: August 26, 2018

Import UnitTest

Class Parametrizedtestcase (UnitTest. TestCase):

"""

After inheriting, rewrite, pass this parameter to unittest inside

"""

def __init__ (self, methodname= ' runtest ', Param=none):

Super (Parametrizedtestcase, self). __init__ (MethodName)

Self.param = param

@staticmethod

def parametrize (Testcase_klass, Param=none):

Testloader = UnitTest. Testloader ()

Testnames = Testloader.gettestcasenames (Testcase_klass)

Suite = UnitTest. TestSuite ()

For name in Testnames:

Suite.addtest (Testcase_klass (name, Param=param))

Return Suite

Class Testone (Parametrizedtestcase):

def test_first (self):

Print (' param = ', self.param)

Self.assertequal (1, 1)

def test_two (self):

Print (' param = ', self.param)

Self.assertequal (2, 2)

if __name__ = = ' __main__ ':

Suite = UnitTest. TestSuite ()

Suite.addtest (Parametrizedtestcase.parametrize (Testone, Param=1))

Suite.addtest (Parametrizedtestcase.parametrize (Testone, param=2))

UnitTest. Texttestrunner (verbosity=2). Run (Suite)

The above method is not found to pass parameters. But maybe a lot of small partners do not know what the meaning, OK, we carefully look at the code of the split, after all, this code a lot of online, but many small partners do not know why, and many small partners do not know how this and our needs in tandem, and do not know what the relationship with us, we should how to deal with. Let's look at the following:

    Suite = UnitTest. TestSuite ()      suite.addtest (Parametrizedtestcase.parametrize (Testone, Param=1))

First we look at our program execution portal, at our entrance we know we have a container, this time we need to add a case to the container, the case is added we are conditional, we first need to know the case in which class inside, In other words, when we add our case, we add the class and the method together, and what does it have to do with what we said above by getting the py file and then thinking about the arguments? First you need to ask a question:

1, our previous code just got all our case, and did not get our class name, the system just through the py file to get the case and then add to a collection

2, we know that the current improvement of the unittest framework knowledge is required by the class name of this we do not seem to be able to operate

There are so two questions then we can change a way of thinking, since the parameters of the current knowledge we can only be manipulated through the above parameters, then how can we change our own code, and then do not we add a case?

In fact, through the above code we have found a different place, we do not need each method of each method to add, only need to have a class name on the line, so we can put all the case into this? The answer is yes.

We just need to pass a simple conversion on it.

Take a closer look at the code above and we know that the code is definitely public, so let's pull this out as a public class:

#-*-coding:utf-8-*-#author: mushishi#date:2018 August 26 Import UnitTest  class Parametrizedtestcase (unittest. TestCase): "" "      after inheriting, rewrite, pass this parameter to UnitTest inside" ""      def __init__ (self, methodname= ' runtest ', param= None):          super (Parametrizedtestcase, self). __init__ (methodName)          self.param = param      @staticmethod      def parametrize (Testcase_klass, Param=none):          testloader = unittest. Testloader ()          testnames = Testloader.gettestcasenames (testcase_klass)          suite = UnitTest. TestSuite () for          name in Testnames:              suite.addtest (Testcase_klass (name, Param=param))          return suite# We're just pulling out of this class, and we're using this as a parameterized, centralized location.

See here, you understand? In fact, we just need to pull this out, and then our case becomes this:

First Case class:

#-*-coding:utf-8-*-#author: mushishi#date:2018 August 26 from case. Parametrizedtestcase Import Parametrizedtestcaseclass Test01 (parametrizedtestcase):    def test_01_case (self):        print ("This is the parameter inside the Test01case", Self.param)    def test_02_case (self):        print ("This is the parameter inside the Test01case", Self.param)

A second case class:

#-*-coding:utf-8-*-#author: mushishi#date:2018 August 26 from case. Parametrizedtestcase Import parametrizedtestcaseimport unittestfrom case.pounittest_one import Test01class TestOne ( Parametrizedtestcase):      def test_first (self):          print (' param = ', self.param)         self.assertequal (1, 1)         def test_two (self):        print (' param = ', self.param)           self.assertequal (2, 2)  if __name__ = = ' __main__ ':    Suite = UnitTest. TestSuite ()      suite.addtest (Parametrizedtestcase.parametrize (Testone, param=1))      suite.addtest ( Parametrizedtestcase.parametrize (Testone, param=2))    suite.addtest (Parametrizedtestcase.parametrize (Test01, param=3))    unittest. Texttestrunner (verbosity=2). Run (Suite)  

Here I have introduced two case classes, but just add such a method, is not solved it? In fact, such a method online a lot, but many need to think about how to do. Or a change of mind.

Appium python unittest pageobject How to implement loading multiple case

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.