2014-01-07 29 views
1

我將一個代碼庫從Ruby轉換爲Python。在Ruby/RSpec的我寫定製「的匹配」,這讓我黑盒測試Web服務這樣的:什麼是基於自定義http測試斷言的良好Python庫?

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

我想編寫代碼來擴展具有相同功能的Python測試框架。當然,我意識到它可能看起來不一樣。它不需要是BDD。 「斷言......」就好了。 pytest是延期的好選擇嗎?有沒有寫這樣的擴展的例子?

+0

可能的重複http://stackoverflow.com/questions/231371/practicing-bdd-with-python – cmotley

+0

我更新了問題,以明確它不是關於BDD:它是關於_any_測試庫,我可以擴展。 – Dogweather

回答

1

是的,pytest是做你需要什麼的好框架。我們使用pytest與requestsPyHamcrest。看看這個例子:

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給你設置和tearDown的可能性。你也可以編寫內聯匹配器has_status,它可以用於簡單的讀取測試斷言。

相關問題