将方法附加到 class
Appending a method to a class
你好,我有一个用于请求的 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
class=请求关键字
在 C# 等中我会使用部分 class 但在 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
您可以继承:
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
任何其他代码。
您可以导入 class,固有的并添加所需的功能
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)
你好,我有一个用于请求的 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
class=请求关键字
在 C# 等中我会使用部分 class 但在 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
您可以继承:
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
任何其他代码。
您可以导入 class,固有的并添加所需的功能
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)