【问题标题】:Appending a method to a class将方法附加到类
【发布时间】:2015-04-02 16:21:41
【问题描述】:

您好,我有一个用于请求的 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 添加到我所在的代码目录中,然后将其附加到导入的类中

class=请求关键字

在 C# 等中,我会使用部分类,但在 Python 中似乎没有那种东西,不知道如何进行

这是我想“追加”到 /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

【问题讨论】:

    标签: python robotframework partial-classes


    【解决方案1】:

    你可以子类:

    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

    【讨论】:

      【解决方案2】:

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

      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)
      

      【讨论】:

      • 嘿,谢谢大家,我会尝试所有这些,看看有什么用。垫我还是不太明白第一个例子看起来像 Ed 发布的那样吗?第二种方法会喜欢这个权利...只需在我的方法之后通过 RequestsKeywords.create_digest_session = create_digest_session 对吗? def create_digest_session(self, alias, url, auth, headers={}, cookies=None,timeout=None) pass RequestsKeywords.create_digest_session = create_digest_session
      • @matsjoyce 第一个例子也是一样,是上面例子的一般形式。对于第二个示例,您将使用您的函数(而不是通过),正如您所说,RequestsKeywords.create_digest_session = create_digest_session。
      • 我收到 inint 错误,看起来我不明白 [错误] 文件“/home/robm/code/python/robotframework/tests/requests/tests/validateDUT. txt':导入测试库'/home/robm/code/python/robotframework/tests/requests/tests/RequestsKeywordsLocal.py'失败:TypeError:调用元类基础模块时出错。__init__()最多需要2个参数(3给定)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-28
      • 2015-04-17
      • 1970-01-01
      • 2011-10-06
      • 2015-10-16
      • 2023-01-15
      相关资源
      最近更新 更多