线上第六期_Python 接口测试入门_20180805

PyEnv

pyenv install -l
pyenv install 2.7.14
pyenv install 3.7.0
pyenv install jython-2.7.1

VirutalEnv

virtualenv --no-site-packages venv
.  venv/bin/activate
pip install selenium

PyTest/unittest + PyHamcrest

import pytest
from hamcrest import *

@pytest.mark.parametrize("actual, expect", [
    (1, 2),
    (1, 1),
    ("aaa", "bbb"),
    ("aaa", "aaa")
])
def test_data(actual, expect):
    assert_that(actual, equal_to(expect))

PyTest-Html

conftest.py

import pytest
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
    pytest_html = item.config.pluginmanager.getplugin('html')
    outcome = yield
    report = outcome.get_result()
    extra = getattr(report, 'extra', [])
    if report.when == 'call':
        # always add url to report
        extra.append(pytest_html.extras.url('https://www.testerhome.com/'))
        xfail = hasattr(report, 'wasxfail')
        if (report.skipped and xfail) or (report.failed and not xfail):
            # only add additional html on failure
            extra.append(pytest_html.extras.html('<div>Additional HTML</div>'))
            extra.append(pytest_html.extras.json({'name': 'pytest'}))
            extra.append(pytest_html.extras.image('https://10.url.cn/eth/ajNVdqHZLLDJO0G7b8ae77GerJNfwYdLNT3SBc7M4EgbWViaH2iaZQ4icZp7efxCtbVib63I8QJ1tQo/'))

        report.extra = extra

Requests

class TesterHome(unittest.TestCase):
    def test_topics(self):
        proxies = {
            'http': 'http://127.0.0.1:8080',
            'https': 'http://127.0.0.1:8080',
        }
        response=requests.get("https://testerhome.com/api/v3/topics.json",
                           params={ "a": 2 },
                           headers={"header1": "header1 content"},
                           cookies={"user": "seveniruby"},
                           proxies=proxies,
                           verify=False
                           )
        print(response.headers)
        print(response.status_code)
        print(response.json())
        print(response.encoding)

        print(len(response.json()["topics"]))

        json=response.json()
        assert_that(json["topics"][0]["title"], contains_string("MTSC2017"))
        assert_that(len(json["topics"]), less_than(20))

```\\