追加的方法一类
问题描述:
你好,我有一个RobotFramework库请求 /usr/local/lib/python2.7/dist-packages/RequestsLibrary/RequestsKeywords.py追加的方法一类
是缺少一些代码来暴露摘要认证方法在Python.Requests库中。我想要做的不是将其入侵到 /usr/local/lib/python2.7/dist-packages/RequestsLibrary/RequestsKeywords.py
我想添加如果到我的代码目录和只是将其追加到入口即化
类的类= RequestsKeywords
在C#等我会用一个局部类,但似乎并没有被这种事情在Python任何想法如何PROCEDE
这里是我想要“APPEND”进入 的代码/usr/local/lib/python2.7/dist-包/ 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
。
答
你可以导入类,内在并添加所需的功能
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)
嘿感谢家伙生病尝试所有这些看看有什么效果.. 垫我还是不太明白,第一个例子会是什么样子埃德发布的是正确的? 第二种方法将罗这样吧...只是我的方法正确后 通 RequestsKeywords.create_digest_session = create_digest_session ? DEF create_digest_session(个体,别名URL,AUTH,标头= {},饼干=无,超时=无) 通 RequestsKeywords.create_digest_session = create_digest_session – RobM 2015-04-03 17:58:25
@matsjoyce第一个例子是相同的,它是一个一般的形式上面的例子。对于第二个示例,您可以像使用函数一样使用函数(而不是通过),正如您所说的那样,请求关键字.create_digest_session = create_digest_session。 – 2015-04-03 19:13:33
我得到一个错误,因为它看起来像我不明白[错误]文件错误'/home/robm/code/python/robotframework/tests/requests/tests/validateDUT.txt':导入测试库' /home/robm/code/python/robotframework/tests/requests/tests/RequestsKeywordsLocal.py'失败:TypeError:错误调用元类基地 模块。__init __()最多需要2个参数(给出3个参数) – RobM 2015-04-07 21:23:16