Beispiel #1
0
 def test_get_block_by_name(self):
     loader.load_test_dependencies()
     ref_call = "get_user($uid, $token)"
     block = loader._get_block_by_name(ref_call, "api")
     self.assertEqual(block["request"]["url"], "/api/users/$uid")
     self.assertEqual(block["function_meta"]["func_name"], "get_user")
     self.assertEqual(block["function_meta"]["args"], ['$uid', '$token'])
Beispiel #2
0
    def test_get_test_definition_suite(self):
        loader.load_test_dependencies()
        api_def = loader._get_test_definition("create_and_check", "suite")
        self.assertEqual(api_def["config"]["name"],
                         "create user and check result.")

        with self.assertRaises(exceptions.SuiteNotFound):
            loader._get_test_definition("create_and_check_XXX", "suite")
Beispiel #3
0
 def test_load_testcases_by_path_layered(self):
     loader.load_test_dependencies()
     path = os.path.join(os.getcwd(), 'tests/data/demo_testset_layer.yml')
     testsets_list = loader.load_testcases(path)
     self.assertIn("variables", testsets_list[0]["config"])
     self.assertIn("request", testsets_list[0]["config"])
     self.assertIn("request", testsets_list[0]["testcases"][0])
     self.assertIn("url", testsets_list[0]["testcases"][0]["request"])
     self.assertIn("validate", testsets_list[0]["testcases"][0])
Beispiel #4
0
    def test_get_test_definition_api(self):
        loader.load_test_dependencies()
        api_def = loader._get_test_definition("get_headers", "api")
        self.assertEqual(api_def["request"]["url"], "/headers")
        self.assertEqual(len(api_def["setup_hooks"]), 2)
        self.assertEqual(len(api_def["teardown_hooks"]), 1)

        with self.assertRaises(exceptions.ApiNotFound):
            loader._get_test_definition("get_token_XXX", "api")
Beispiel #5
0
 def test_load_test_file_testcase(self):
     loader.load_test_dependencies()
     testset = loader.load_test_file("tests/testcases/smoketest.yml")
     self.assertEqual(testset["config"]["name"], "smoketest")
     self.assertEqual(testset["config"]["path"],
                      "tests/testcases/smoketest.yml")
     self.assertIn("device_sn", testset["config"]["variables"][0])
     self.assertEqual(len(testset["testcases"]), 8)
     self.assertEqual(testset["testcases"][0]["name"], "get token")
Beispiel #6
0
    def test_load_testcases_by_path_folder(self):
        loader.load_test_dependencies()
        # absolute folder path
        path = os.path.join(os.getcwd(), 'tests/data')
        testset_list_1 = loader.load_testcases(path)
        self.assertGreater(len(testset_list_1), 4)

        # relative folder path
        path = 'tests/data/'
        testset_list_2 = loader.load_testcases(path)
        self.assertEqual(len(testset_list_1), len(testset_list_2))

        # list/set container with file(s)
        path = [os.path.join(os.getcwd(), 'tests/data'), 'tests/data/']
        testset_list_3 = loader.load_testcases(path)
        self.assertEqual(len(testset_list_3), 2 * len(testset_list_1))
Beispiel #7
0
    def test_override_block(self):
        loader.load_test_dependencies()
        def_block = loader._get_block_by_name(
            "get_token($user_agent, $device_sn, $os_platform, $app_version)",
            "api")
        test_block = {
            "name":
            "override block",
            "variables": [{
                "var": 123
            }],
            'request': {
                'url': '/api/get-token',
                'method': 'POST',
                'headers': {
                    'user_agent': '$user_agent',
                    'device_sn': '$device_sn',
                    'os_platform': '$os_platform',
                    'app_version': '$app_version'
                },
                'json': {
                    'sign':
                    '${get_sign($user_agent, $device_sn, $os_platform, $app_version)}'
                }
            },
            'validate': [{
                'eq': ['status_code', 201]
            }, {
                'len_eq': ['content.token', 32]
            }]
        }

        utils._override_block(def_block, test_block)
        self.assertEqual(test_block["name"], "override block")
        self.assertIn(
            {
                'check': 'status_code',
                'expect': 201,
                'comparator': 'eq'
            }, test_block["validate"])
        self.assertIn(
            {
                'check': 'content.token',
                'comparator': 'len_eq',
                'expect': 32
            }, test_block["validate"])
Beispiel #8
0
def gen_locustfile(testcase_file_path):
    """ generate locustfile from template.
    """
    locustfile_path = 'locustfile.py'
    template_path = os.path.join(
        os.path.dirname(os.path.realpath(__file__)),
        "templates",
        "locustfile_template"
    )
    loader.load_test_dependencies()
    testset = loader.load_test_file(testcase_file_path)
    host = testset.get("config", {}).get("request", {}).get("base_url", "")

    with io.open(template_path, encoding='utf-8') as template:
        with io.open(locustfile_path, 'w', encoding='utf-8') as locustfile:
            template_content = template.read()
            template_content = template_content.replace("$HOST", host)
            template_content = template_content.replace("$TESTCASE_FILE", testcase_file_path)
            locustfile.write(template_content)

    return locustfile_path
Beispiel #9
0
def init_test_suites(path_or_testsets, mapping=None, http_client_session=None):
    """ initialize TestSuite list with testset path or testset dict
    @params
        testsets (dict/list): testset or list of testset
            testset_dict
            or
            [
                testset_dict_1,
                testset_dict_2,
                {
                    "config": {},
                    "api": {},
                    "testcases": [testcase11, testcase12]
                }
            ]
        mapping (dict):
            passed in variables mapping, it will override variables in config block
    """
    if not testcase.is_testsets(path_or_testsets):
        loader.load_test_dependencies()
        testsets = loader.load_testcases(path_or_testsets)
    else:
        testsets = path_or_testsets

    # TODO: move comparator uniform here
    mapping = mapping or {}

    if not testsets:
        raise exceptions.TestcaseNotFound

    if isinstance(testsets, dict):
        testsets = [testsets]

    test_suite_list = []
    for testset in testsets:
        test_suite = TestSuite(testset, mapping, http_client_session)
        test_suite_list.append(test_suite)

    return test_suite_list
Beispiel #10
0
 def test_get_block_by_name_args_mismatch(self):
     loader.load_test_dependencies()
     ref_call = "get_user($uid, $token, $var)"
     with self.assertRaises(exceptions.ParamsError):
         loader._get_block_by_name(ref_call, "api")
Beispiel #11
0
 def test_load_test_dependencies(self):
     loader.load_test_dependencies()
     overall_def_dict = loader.overall_def_dict
     self.assertIn("get_token", overall_def_dict["api"])
     self.assertIn("create_and_check", overall_def_dict["suite"])