繁体   English   中英

有什么好的自定义http测试断言可基于的Python库?

[英]What is a good Python library on which to base custom http test assertions?

我正在将代码库从Ruby转换为Python。 在Ruby / RSpec中,我编写了自定义的“匹配器”,使我可以对这样的Web服务进行黑盒测试:

describe 'webapp.com' do
  it 'is configured for ssl' do
    expect('www.webapp.com').to have_a_valid_cert
  end
end

我想编写代码以扩展具有相同功能的Python测试框架。 我意识到,当然可能看起来不一样。 不需要是BDD。 “声明...”很好。 pytestpytest的好选择吗? 是否有编写此类扩展的示例?

是的,pytest是满足您需求的良好框架。 我们正在将pytest与requestPyHamcrest一起使用 看这个例子:

import pytest
import requests
from hamcrest import *

class SiteImpl:
    def __init__(self, url):
        self.url = url

    def has_valid_cert(self):
        return requests.get(self.url, verify=True)

@pytest.yield_fixture
def site(request):
    # setUp
    yield SiteImpl('https://' + request.param)
    # tearDown

def has_status(item):
    return has_property('status_code', item)

@pytest.mark.parametrize('site', ['google.com', 'github.com'], indirect=True)
def test_cert(site):
    assert_that(site.has_valid_cert(), has_status(200))

if __name__ == '__main__':
    pytest.main(args=[__file__, '-v'])

上面的代码对夹具site使用参数化。 yeld_fixture使您可以进行setUp和tearDown。 您也可以编写内联匹配器has_status ,可用于轻松读取测试断言。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM