自动化测试平台化[v1.0.0][面向对象]

封装

# encoding = utf-8

"""
面向对象第一大特征:封装
基于Python3
"""


class CellPhone:
    """
    手机类
    """
    def __init__(self, cell_phone_number):
        self.cell_phone_number = cell_phone_number
        self.battery_percentage = 100

    def dial(self, cell_phone_number):
        print("Calling %s" % cell_phone_number)

    def send_sms(self, cell_phone_number, message):
        print("Sending %s to %s" % (message, cell_phone_number))

    def start_charge(self):
        print("Charging...")

    def stop_charge(self):
        print("Charge Finished")


if __name__ == '__main__':
    P30 = CellPhone("159xxxxxxxx")
    P40 = CellPhone("180xxxxxxxx")
    print("P30 手机号是 %s" % P30.cell_phone_number)
    print("P30 手机还剩余电量 %d" % P30.battery_percentage)

    P40.battery_percentage = 50
    print("P40 手机号是 %s" % P40.cell_phone_number)
    print("P40 手机还剩余电量 %d" % P40.battery_percentage)

    P30.dial(P40.cell_phone_number)
    P40.send_sms(P30.cell_phone_number, "Give u feedback later")

继承

# encoding = utf-8
from OOP import CellPhone


class SymbianMobilePhone(CellPhone):
    """
    塞班手机
    """
    pass


class SmartMobilePhone(CellPhone):
    """
    智能手机
    """
    def __init__(self, cell_phone_number, os="Android"):
        super().__init__(cell_phone_number)
        self.os = os
        self.app_list = list()
    
    def download_app(self, app_name):
        print("正在下载应用 %s" % app_name)
        
    def delete_app(self, app_name):
        print("正在删除应用 %s" % app_name)
        

class FullSmartMobilePhone(SmartMobilePhone):
    """
    全面屏智能手机
    """
    def __init__(self, cell_phone_number, screen_size, os="Android"):
        super().__init__(cell_phone_number, os)
        self.screen_size = screen_size


class FolderScreenSmartMobilePhone(SmartMobilePhone):
    """
    折叠屏智能手机
    """
    def fold(self):
        print("The CellPhone is folded")
        
    def unfold(self):
        print("The CellPhone is unfolded")

多态

# encoding = utf-8
class IPhone:
    """
    IPhone基类,具有一个解锁功能
    """
    def unlock(self, pass_code, **kwargs):
        print("解锁IPhone")
        return True


class IPhone5S(IPhone):
    """
    IPhone5S,unlock功能增加了指纹解锁
    """
    def finger_unlock(self, fingerprint):
        return True

    def unlock(self, pass_code, **kwargs):
        fingerprint = kwargs.get("fingerprint", None)
        if self.finger_unlock(fingerprint):
            print("指纹解锁成功")
            return True
        else:
            return super().unlock(pass_code)


class IPhoneX(IPhone):
    """
    IPhoneX, unlock功能增加刷脸解锁
    """
    def face_unlock(self, face_id):
        return True

    def unlock(self, pass_code, **kwargs):
        face_id = kwargs.get("face_id", None)
        if self.face_unlock(face_id):
            print("通过刷脸解锁成功")
            return True
        else:
            super().unlock(pass_code)

抽象

from abc import ABCMeta, abstractmethod

class MobilePhone(metaclass=ABCMeta):

    @abstractmethod
    def unlock(self, credential):
        pass

class IPhone(MobilePhone):
    def unlock(self, credential):
        print("IPhone解锁")

class IPhone5S(MobilePhone):
    def unlock(self, credential):
        print("5S解锁")

class IPhoneX(MobilePhone):
    def unlock(self, credential):
        print("IPhoneX解锁")

def test_unlock(phone):
    if isinstance(phone, IPhone):
        phone.unlock("......")
    else:
        print("传入的参数必须是MobilePhone类型")
        return False


if __name__ == '__main__':
    phone = IPhone()
    test_unlock(phone)

你可能感兴趣的:(自动化测试平台化)