当前位置

网站首页> 程序设计 > 开源项目 > 编程语言 > 浏览文章

HTTP 交互模拟库 betamax

作者:小梦 来源: 网络 时间: 2024-06-19 阅读:

Betamax 是 VCR 的模仿,用于 HTTP 请求。使用方法:

from betamax import Betamaxfrom requests import Sessionfrom unittest import TestCasewith Betamax.configure() as config:    config.cassette_library_dir = 'tests/fixtures/cassettes'class TestGitHubAPI(TestCase):    def setUp(self):        self.session = Session()        self.headers.update(...)    # Set the cassette in a line other than the context declaration    def test_user(self):        with Betamax(self.session) as vcr:            vcr.use_cassette('user')            resp = self.session.get('https://api.github.com/user',                                    auth=('user', 'pass'))            assert resp.json()['login'] is not None    # Set the cassette in line with the context declaration    def test_repo(self):        with Betamax(self.session).use_cassette('repo'):            resp = self.session.get(                'https://api.github.com/repos/sigmavirus24/github3.py'                )            assert resp.json()['owner'] != {}

相关阅读

热点阅读

网友最爱