将方法附加到类

时间:2015-04-02 16:21:41

标签: python robotframework partial-classes

您好我有一个RobotFramework库来处理请求 /usr/local/lib/python2.7/dist-packages/RequestsLibrary/RequestsKeywords.py

缺少一些代码来公开Python.Requests库中的Digest身份验证方法。我想做的不是黑客入侵 /usr/local/lib/python2.7/dist-packages/RequestsLibrary/RequestsKeywords.py

我想将if添加到我所在的代码目录中,并将其附加到导入的类

类= RequestsKeywords

在C#等中我会使用一个局部类,但在Python中似乎没有任何想法如何处理

这是我想要的代码" APPEND"进入 /usr/local/lib/python2.7/dist-packages/RequestsLibrary/RequestsKeywords.py 文库

def create_digest_session(self, alias, url, auth, headers={}, cookies=None, timeout=None, proxies=None, verify=False):

   """ Create Session: create a HTTP session to a server

   `url` Base url of the server

   `alias` Robot Framework alias to identify the session

   `headers` Dictionary of default headers

   `auth` List of username & password for HTTP Digest Auth

   `timeout` connection timeout

   `proxies` proxy server url

   `verify` set to True if Requests should verify the certificate
   """
   digest_auth = requests.auth.HTTPDigestAuth(*auth) if auth else None
   return self._create_session(alias, url, headers, cookie, digest_auth, timeout, proxies, verify)

任何建议

所以我的测试如下所示,你用sys.path.append指定的python代码将在RequestsKeywordsLocal.py文件中 正确的吗?

*** Settings ***
Suite Teardown    Delete All Sessions
Library           Collections
Library           String
Library           /usr/local/lib/python2.7/dist-packages/RequestsLibrary/RequestsKeywords.py
Library           ./RequestsKeywordsLocal.py 
Library           ./localKeywords.py 
Library           OperatingSystem
Library           BuiltIn

*** Variables ***
${HEADER1}        Content-Type: text/xml; charset=UTF-8
${IP}             172.168.101.139
${UN}             username
${PW}             password

*** Test Cases ***
Check IP valid
    Valid Ip   ${IP}

Get Valid Hostname
    ${host}=    Valid Hostname   ${IP}
    Should Not Be Equal As Strings  ${host}  "noname"

Get With DigestAuth
    [Tags]    get
    Log    Variables
    ${auth}=    Create List    username    password
    Create Digest Session    TerraceQ    https://${IP}    auth=${auth}
    ${resp}=    Get    TerraceQ    /views
    Should Be Equal As Strings    ${resp.status_code}    200
    Should Contain  ${resp.content}  views

2 个答案:

答案 0 :(得分:2)

您可以选择子类:

class MyNewXThing(XThing):
    def my_added_method(self, x, y, z):
        # ...

在整个代码中使用MyNewXThing代替XThing。或者:

def my_added_method(self, x, y, z):
    # ...
XThing.my_added_method = my_added_method

第一个选项更灵活,因为它不会为任何其他代码更改XThing

答案 1 :(得分:1)

您可以导入固有的类并添加所需的功能

sys.path.append('/usr/local/lib/python2.7/dist-packages/RequestsLibrary/')
import RequestsKeywords as RequestsKeywords


class RequestsKeywords_local(RequestsKeywords):

    def create_digest_session(self, alias, url, auth, headers={}, cookies=None, timeout=None, proxies=None, verify=False):

       """ Create Session: create a HTTP session to a server

       `url` Base url of the server

       `alias` Robot Framework alias to identify the session

       `headers` Dictionary of default headers

       `auth` List of username & password for HTTP Digest Auth

       `timeout` connection timeout

       `proxies` proxy server url

       `verify` set to True if Requests should verify the certificate
       """
       digest_auth = requests.auth.HTTPDigestAuth(*auth) if auth else None
       return self._create_session(alias, url, headers, cookie, digest_auth, timeout, proxies, verify)

    def __init__(self,**kwargs):

        RequestsKeywords.__init__(self,**kwargs)