示例#1
0
class TestCaseRequestWithTestcaseReference(HttpRunner):

    config = (Config("request methods testcase: reference testcase").variables(
        **{
            "foo1": "testsuite_config_bar1",
            "expect_foo1": "testsuite_config_bar1",
            "expect_foo2": "config_bar2",
        }).base_url("https://postman-echo.com").verify(False))

    teststeps = [
        Step(
            RunTestCase("request with functions").with_variables(
                **{
                    "foo1": "testcase_ref_bar1",
                    "expect_foo1": "testcase_ref_bar1"
                }).setup_hook("${sleep(0.1)}").call(RequestWithFunctions).
            teardown_hook("${sleep(0.2)}").export(*["foo3"])),
        Step(
            RunRequest("post form data").with_variables(**{
                "foo1": "bar1"
            }).post("/post").with_headers(
                **{
                    "User-Agent": "HttpRunner/${get_httprunner_version()}",
                    "Content-Type": "application/x-www-form-urlencoded",
                }).with_data("foo1=$foo1&foo2=$foo3").validate().assert_equal(
                    "status_code",
                    200).assert_equal("body.form.foo1", "bar1").assert_equal(
                        "body.form.foo2", "bar21")),
    ]
示例#2
0
class TestCaseValidateWithFunctions(HttpRunner):

    config = (
        Config("request methods testcase: validate with functions")
        .variables(**{"foo1": "session_bar1"})
        .base_url("https://postman-echo.com")
        .verify(False)
    )

    teststeps = [
        Step(
            RunRequest("get with params")
            .with_variables(
                **{"foo1": "bar1", "foo2": "session_bar2", "sum_v": "${sum_two(1, 2)}"}
            )
            .get("/get")
            .with_params(**{"foo1": "$foo1", "foo2": "$foo2", "sum_v": "$sum_v"})
            .with_headers(**{"User-Agent": "HttpRunner/${get_httprunner_version()}"})
            .extract()
            .with_jmespath("body.args.foo2", "session_foo2")
            .validate()
            .assert_equal("status_code", 200)
            .assert_equal("body.args.sum_v", "3")
        ),
    ]
示例#3
0
class TestCaseUpload(HttpRunner):

    config = Config("test upload file with httpbin").base_url("${get_httpbin_server()}")

    teststeps = [
        Step(
            RunRequest("upload file")
            .with_variables(
                **{
                    "file_path": "test.env",
                    "m_encoder": "${multipart_encoder(file=$file_path)}",
                }
            )
            .post("/post")
            .with_headers(**{"Content-Type": "${multipart_content_type($m_encoder)}"})
            .with_data("$m_encoder")
            .validate()
            .assert_equal("status_code", 200)
            .assert_startswith("body.files.file", "UserName=test")
        ),
        Step(
            RunRequest("upload file with keyword")
            .post("/post")
            .upload(**{"file": "test.env"})
            .validate()
            .assert_equal("status_code", 200)
            .assert_startswith("body.files.file", "UserName=test")
        ),
    ]
示例#4
0
class TestCaseValidateWithVariables(HttpRunner):

    config = (
        Config("request methods testcase: validate with variables").variables(
            **{
                "foo1": "session_bar1"
            }).base_url("https://postman-echo.com").verify(False))

    teststeps = [
        Step(
            RunRequest("get with params").with_variables(**{
                "foo1": "bar1",
                "foo2": "session_bar2"
            }).get("/get").with_params(**{
                "foo1": "$foo1",
                "foo2": "$foo2"
            }).with_headers(**{
                "User-Agent": "HttpRunner/3.0"
            }).extract().with_jmespath("body.args.foo2",
                                       "session_foo2").validate().assert_equal(
                                           "status_code", 200).assert_equal(
                                               "body.args.foo1",
                                               "$foo1").assert_equal(
                                                   "body.args.foo2", "$foo2")),
        Step(
            RunRequest("post raw text").with_variables(**{
                "foo1": "hello world",
                "foo3": "$session_foo2"
            }).post("/post").with_headers(**{
                "User-Agent": "HttpRunner/3.0",
                "Content-Type": "text/plain"
            }).with_data(
                "This is expected to be sent back as part of response body: $foo1-$foo3."
            ).validate().assert_equal("status_code", 200).assert_equal(
                "body.data",
                "This is expected to be sent back as part of response body: hello world-$foo3.",
            )),
        Step(
            RunRequest("post form data").with_variables(**{
                "foo1": "bar1",
                "foo2": "bar2"
            }).post("/post").with_headers(
                **{
                    "User-Agent": "HttpRunner/3.0",
                    "Content-Type": "application/x-www-form-urlencoded",
                }).with_data("foo1=$foo1&foo2=$foo2").validate().assert_equal(
                    "status_code",
                    200).assert_equal("body.form.foo1", "$foo1").assert_equal(
                        "body.form.foo2", "$foo2")),
    ]
示例#5
0
class TestCaseLoadImage(HttpRunner):

    config = Config("load images").base_url("${get_httpbin_server()}")

    teststeps = [
        Step(
            RunRequest("get png image").get(
                "/image/png").validate().assert_equal("status_code", 200)),
        Step(
            RunRequest("get jpeg image").get(
                "/image/jpeg").validate().assert_equal("status_code", 200)),
        Step(
            RunRequest("get webp image").get(
                "/image/webp").validate().assert_equal("status_code", 200)),
        Step(
            RunRequest("get svg image").get(
                "/image/svg").validate().assert_equal("status_code", 200)),
    ]
示例#6
0
class TestCaseValidate(HttpRunner):

    config = Config("basic test with httpbin").base_url("http://httpbin.org/")

    teststeps = [
        Step(
            RunRequest("validate response with json path").get(
                "/get").with_params(**{
                    "a": 1,
                    "b": 2
                }).validate().assert_equal("status_code", 200).assert_equal(
                    "body.args.a", "1").assert_equal("body.args.b", "2")),
        Step(
            RunRequest("validate response with python script").get(
                "/get").with_params(**{
                    "a": 1,
                    "b": 2
                }).validate().assert_equal("status_code", 200)),
    ]
示例#7
0
class TestCaseHooks(HttpRunner):

    config = Config("basic test with httpbin").base_url(
        "${get_httpbin_server()}")

    teststeps = [
        Step(
            RunRequest("headers").with_variables(**{
                "a": 123
            }).setup_hook("${setup_hook_add_kwargs($request)}").setup_hook(
                "${setup_hook_remove_kwargs($request)}").get("/headers").
            teardown_hook("${teardown_hook_sleep_N_secs($response, 1)}").
            validate().assert_equal("status_code", 200).assert_contained_by(
                "body.headers.Host", "${get_httpbin_server()}")),
        Step(
            RunRequest("alter response").get("/headers").teardown_hook(
                "${alter_response($response)}").validate().assert_equal(
                    "status_code", 500).assert_equal('headers."Content-Type"',
                                                     "html/text").assert_equal(
                                                         "body.headers.Host",
                                                         "127.0.0.1:8888")),
    ]
示例#8
0
class TestCaseHardcode(HttpRunner):

    config = (Config("request methods testcase in hardcode").base_url(
        "https://postman-echo.com").verify(False))

    teststeps = [
        Step(
            RunRequest("get with params").get("/get").with_params(
                **{
                    "foo1": "bar1",
                    "foo2": "bar2"
                }).with_headers(**{
                    "User-Agent": "HttpRunner/3.0"
                }).validate().assert_equal("status_code", 200)),
        Step(
            RunRequest("post raw text").post("/post").
            with_headers(**{
                "User-Agent": "HttpRunner/3.0",
                "Content-Type": "text/plain"
            }).with_data(
                "This is expected to be sent back as part of response body.").
            validate().assert_equal("status_code", 200)),
        Step(
            RunRequest("post form data").post("/post").with_headers(
                **{
                    "User-Agent": "HttpRunner/3.0",
                    "Content-Type": "application/x-www-form-urlencoded",
                }).with_data("foo1=bar1&foo2=bar2").validate().assert_equal(
                    "status_code", 200)),
        Step(
            RunRequest("put request").put("/put").with_headers(
                **{
                    "User-Agent": "HttpRunner/3.0",
                    "Content-Type": "text/plain"
                }).
            with_data(
                "This is expected to be sent back as part of response body.").
            validate().assert_equal("status_code", 200)),
    ]
示例#9
0
class TestCaseT23(HttpRunner):

    config = (Config("reference testcase unittest for abnormal folder path").
              base_url("https://postman-echo.com").verify(False))

    teststeps = [
        Step(
            RunTestCase("request with functions").call(T1).export(
                *["session_foo2"])),
        Step(
            RunRequest("post form data").with_variables(**{
                "foo1": "bar12"
            }).post("/post").with_headers(
                **{
                    "User-Agent": "HttpRunner/${get_httprunner_version()}",
                    "Content-Type": "application/x-www-form-urlencoded",
                }).with_data(
                    "foo1=$foo1&foo2=$session_foo2").validate().assert_equal(
                        "status_code", 200).assert_equal(
                            "body.form.foo1",
                            "bar12").assert_equal("body.form.foo2",
                                                  "config_bar2")),
    ]
示例#10
0
class TestCaseRequestWithParameters(HttpRunner):
    @pytest.mark.parametrize(
        "param",
        Parameters({
            "user_agent": ["iOS/10.1", "iOS/10.2"],
            "username-password":
            "******",
            "app_version": "${get_app_version()}",
        }),
    )
    def test_start(self, param):
        super().test_start(param)

    config = (
        Config("request methods testcase: validate with parameters").variables(
            **{
                "app_version": "f1"
            }).base_url("https://postman-echo.com").verify(False))

    teststeps = [
        Step(
            RunRequest("get with params").with_variables(
                **{
                    "foo1": "$username",
                    "foo2": "$password",
                    "sum_v": "${sum_two(1, $app_version)}",
                }).get("/get").with_params(**{
                    "foo1": "$foo1",
                    "foo2": "$foo2",
                    "sum_v": "$sum_v"
                }).with_headers(**{
                    "User-Agent": "$user_agent,$app_version"
                }).extract().with_jmespath(
                    "body.args.foo2", "session_foo2").validate().assert_equal(
                        "status_code", 200).assert_string_equals(
                            "body.args.sum_v", "${sum_two(1, $app_version)}")),
    ]
class TestCaseRequestWithFunctions(HttpRunner):

    config = (Config("request methods testcase with functions").variables(
        **{
            "foo1": "config_bar1",
            "foo2": "config_bar2",
            "expect_foo1": "config_bar1",
            "expect_foo2": "config_bar2",
        }).base_url("https://postman-echo.com").verify(False).export(
            *["foo3"]).locust_weight(2))

    teststeps = [
        Step(
            RunRequest("get with params").with_variables(
                **{
                    "foo1": "bar11",
                    "foo2": "bar21",
                    "sum_v": "${sum_two(1, 2)}"
                }).get("/get").with_params(**{
                    "foo1": "$foo1",
                    "foo2": "$foo2",
                    "sum_v": "$sum_v"
                }).with_headers(
                    **{
                        "User-Agent": "HttpRunner/${get_httprunner_version()}"
                    }).extract().with_jmespath(
                        "body.args.foo2", "foo3").validate().assert_equal(
                            "status_code", 200).assert_equal(
                                "body.args.foo1", "bar11").assert_equal(
                                    "body.args.sum_v",
                                    "3").assert_equal("body.args.foo2",
                                                      "bar21")),
        Step(
            RunRequest("post raw text").with_variables(**{
                "foo1": "bar12",
                "foo3": "bar32"
            }).post("/post").with_headers(
                **{
                    "User-Agent": "HttpRunner/${get_httprunner_version()}",
                    "Content-Type": "text/plain",
                }).
            with_data(
                "This is expected to be sent back as part of response body: $foo1-$foo2-$foo3."
            ).validate().assert_equal("status_code", 200).assert_equal(
                "body.data",
                "This is expected to be sent back as part of response body: bar12-$expect_foo2-bar32.",
            ).assert_type_match("body.json", "None").assert_type_match(
                "body.json", "NoneType").assert_type_match("body.json", None)),
        Step(
            RunRequest("post form data").with_variables(**{
                "foo2": "bar23"
            }).post("/post").with_headers(
                **{
                    "User-Agent": "HttpRunner/${get_httprunner_version()}",
                    "Content-Type": "application/x-www-form-urlencoded",
                }).with_data("foo1=$foo1&foo2=$foo2&foo3=$foo3").validate().
            assert_equal("status_code", 200,
                         "response status code should be 200").assert_equal(
                             "body.form.foo1", "$expect_foo1").assert_equal(
                                 "body.form.foo2",
                                 "bar23").assert_equal("body.form.foo3",
                                                       "bar21")),
    ]
示例#12
0
class TestCaseBasic(HttpRunner):

    config = Config("basic test with httpbin").base_url("https://httpbin.org/")

    teststeps = [
        Step(
            RunRequest("headers")
            .get("/headers")
            .validate()
            .assert_equal("status_code", 200)
            .assert_equal("body.headers.Host", "httpbin.org")
        ),
        Step(
            RunRequest("user-agent")
            .get("/user-agent")
            .validate()
            .assert_equal("status_code", 200)
            .assert_startswith('body."user-agent"', "python-requests")
        ),
        Step(
            RunRequest("get without params")
            .get("/get")
            .validate()
            .assert_equal("status_code", 200)
            .assert_equal("body.args", {})
        ),
        Step(
            RunRequest("get with params in url")
            .get("/get?a=1&b=2")
            .validate()
            .assert_equal("status_code", 200)
            .assert_equal("body.args", {"a": "1", "b": "2"})
        ),
        Step(
            RunRequest("get with params in params field")
            .get("/get")
            .with_params(**{"a": 1, "b": 2})
            .validate()
            .assert_equal("status_code", 200)
            .assert_equal("body.args", {"a": "1", "b": "2"})
        ),
        Step(
            RunRequest("set cookie")
            .get("/cookies/set?name=value")
            .validate()
            .assert_equal("status_code", 200)
            .assert_equal("body.cookies.name", "value")
        ),
        Step(
            RunRequest("extract cookie")
            .get("/cookies")
            .validate()
            .assert_equal("status_code", 200)
            .assert_equal("body.cookies.name", "value")
        ),
        Step(
            RunRequest("post data")
            .post("/post")
            .with_headers(**{"Content-Type": "application/json"})
            .with_data("abc")
            .validate()
            .assert_equal("status_code", 200)
        ),
        Step(
            RunRequest("validate body length")
            .get("/spec.json")
            .validate()
            .assert_length_equal("body", 9)
        ),
    ]