예제 #1
0
    def test_load_testcases_by_path_folder(self):
        # 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))
예제 #2
0
    def test_load_testcases_by_path_folder(self):
        # absolute folder path
        path = os.path.join(os.getcwd(), 'tests/data')
        testset_list_1 = loader.load_testcases(path)
        assert len(testset_list_1) > 1

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

        # list/set container with folder(s)
        path = [os.path.join(os.getcwd(), 'tests/data'), 'tests/data/']
        testset_list_3 = loader.load_testcases(path)
        assert len(testset_list_3) == 2 * len(testset_list_1)
예제 #3
0
 def test_load_testcases_by_path_layered(self):
     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]["teststeps"][0])
     self.assertIn("url", testsets_list[0]["teststeps"][0]["request"])
     self.assertIn("validate", testsets_list[0]["teststeps"][0])
예제 #4
0
 def test_load_testcases_by_path_layered(self):
     path = os.path.join(os.getcwd(), 'tests/data/demo_testset_layer.yml')
     testsets_list = loader.load_testcases(path)
     assert 'variables' in testsets_list[0]['config']
     assert 'request' in testsets_list[0]['config']
     assert 'request' in testsets_list[0]['teststeps'][0]
     assert 'url' in testsets_list[0]['teststeps'][0]['request']
     assert 'validate' in testsets_list[0]['teststeps'][0]
예제 #5
0
 def test_run_testcase_with_empty_header(self):
     testcase_file_path = os.path.join(os.getcwd(),
                                       'tests/data/test_bugfix.yml')
     testsets = loader.load_testcases(testcase_file_path)
     testset = testsets[0]
     config_dict_headers = testset["config"]["request"]["headers"]
     test_dict_headers = testset["teststeps"][0]["request"]["headers"]
     headers = deep_update_dict(config_dict_headers, test_dict_headers)
     self.assertEqual(headers["Content-Type"], "application/json")
예제 #6
0
    def test_load_testcases_by_path_files(self):
        testsets_list = []

        # absolute file path
        path = os.path.join(os.getcwd(),
                            'tests/data/demo_testset_hardcode.json')
        testset_list = loader.load_testcases(path)
        self.assertEqual(len(testset_list), 1)
        self.assertIn("path", testset_list[0]["config"])
        self.assertEqual(testset_list[0]["config"]["path"], path)
        self.assertEqual(len(testset_list[0]["teststeps"]), 3)
        testsets_list.extend(testset_list)

        # relative file path
        path = 'tests/data/demo_testset_hardcode.yml'
        testset_list = loader.load_testcases(path)
        self.assertEqual(len(testset_list), 1)
        self.assertIn("path", testset_list[0]["config"])
        self.assertIn(path, testset_list[0]["config"]["path"])
        self.assertEqual(len(testset_list[0]["teststeps"]), 3)
        testsets_list.extend(testset_list)

        # list/set container with file(s)
        path = [
            os.path.join(os.getcwd(), 'tests/data/demo_testset_hardcode.json'),
            'tests/data/demo_testset_hardcode.yml'
        ]
        testset_list = loader.load_testcases(path)
        self.assertEqual(len(testset_list), 2)
        self.assertEqual(len(testset_list[0]["teststeps"]), 3)
        self.assertEqual(len(testset_list[1]["teststeps"]), 3)
        testsets_list.extend(testset_list)
        self.assertEqual(len(testsets_list), 4)

        for testset in testsets_list:
            for test in testset["teststeps"]:
                self.assertIn('name', test)
                self.assertIn('request', test)
                self.assertIn('url', test['request'])
                self.assertIn('method', test['request'])
예제 #7
0
    def test_load_testcases_by_path_files(self):
        testsets_list = []

        # absolute file path
        path = os.path.join(os.getcwd(),
                            'tests/data/demo_testset_hardcode.json')
        testset_list = loader.load_testcases(path)
        assert len(testset_list) == 1
        assert len(testset_list[0]['teststeps']) == 3
        testsets_list.extend(testset_list)

        # relative file path
        path = 'tests/data/demo_testset_hardcode.yml'
        testset_list = loader.load_testcases(path)
        assert len(testset_list) == 1
        assert len(testset_list[0]['teststeps']) == 3
        testsets_list.extend(testset_list)

        # list/set container with file(s)
        path = [
            os.path.join(os.getcwd(), 'tests/data/demo_testset_hardcode.json'),
            'tests/data/demo_testset_hardcode.yml'
        ]
        testset_list = loader.load_testcases(path)
        assert len(testset_list) == 2
        assert len(testset_list[0]['teststeps']) == 3
        assert len(testset_list[1]['teststeps']) == 3
        testsets_list.extend(testset_list)
        assert len(testsets_list) == 4

        for testset in testsets_list:
            for test in testset['teststeps']:
                assert 'name' in test
                assert 'request' in test
                assert 'url' in test['request']
                assert 'method' in test['request']
예제 #8
0
파일: task.py 프로젝트: zdmd/HttpRunner
def init_test_suites(path_or_testcases, mapping=None, http_client_session=None):
    """ initialize TestSuite list with testcase path or testcase(s).

    Args:
        path_or_testcases (str/dict/list): testcase file path or testcase dict or testcases list

            testcase_dict
            or
            [
                testcase_dict_1,
                testcase_dict_2,
                {
                    "config": {},
                    "teststeps": [teststep11, teststep12]
                }
            ]

        mapping (dict): passed in variables mapping, it will override variables in config block.
        http_client_session (instance): requests.Session(), or locusts.client.Session() instance.

    Returns:
        list: TestSuite() instance list.

    """
    if validator.is_testcases(path_or_testcases):
        testcases = path_or_testcases
    else:
        testcases = loader.load_testcases(path_or_testcases)

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

    if not testcases:
        raise exceptions.TestcaseNotFound

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

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

    return test_suite_list
예제 #9
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")
    testcases = loader.load_testcases(testcase_file_path)
    host = testcases[0].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
예제 #10
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
예제 #11
0
    def test_load_testcases_by_path_not_exist(self):
        # absolute folder path
        path = os.path.join(os.getcwd(), 'tests/data_not_exist')
        with self.assertRaises(exceptions.FileNotFound):
            loader.load_testcases(path)

        # relative folder path
        path = 'tests/data_not_exist'
        with self.assertRaises(exceptions.FileNotFound):
            loader.load_testcases(path)

        # list/set container with file(s)
        path = [
            os.path.join(os.getcwd(), 'tests/data_not_exist'),
            'tests/data_not_exist/'
        ]
        with self.assertRaises(exceptions.FileNotFound):
            loader.load_testcases(path)
예제 #12
0
    def load_tests(self, path_or_testcases):
        """ load testcases, extend and merge with api/testcase definitions.

        Args:
            path_or_testcases (str/dict/list): YAML/JSON testcase file path or testcase list
                path (str): testcase file/folder path
                testcases (dict/list): testcase dict or list of testcases

        Returns:
            list: valid testcases list.

                [
                    # testcase data structure
                    {
                        "config": {
                            "name": "desc1",
                            "path": "",         # optional
                            "variables": [],    # optional
                            "request": {}       # optional
                        },
                        "teststeps": [
                            # teststep data structure
                            {
                                'name': 'test step desc2',
                                'variables': [],    # optional
                                'extract': [],      # optional
                                'validate': [],
                                'request': {},
                                'function_meta': {}
                            },
                            teststep2   # another teststep dict
                        ]
                    },
                    {}  # another testcase dict
                ]

        """
        if validator.is_testcases(path_or_testcases):
            # TODO: refactor
            if isinstance(path_or_testcases, list):
                for testcase in path_or_testcases:
                    try:
                        test_path = os.path.dirname(testcase["config"]["path"])
                    except KeyError:
                        test_path = os.getcwd()
                    loader.load_project_tests(test_path)
            else:
                try:
                    test_path = os.path.dirname(
                        path_or_testcases["config"]["path"])
                except KeyError:
                    test_path = os.getcwd()
                loader.load_project_tests(test_path)

            testcases = path_or_testcases
        else:
            testcases = loader.load_testcases(path_or_testcases)

        self.project_mapping = loader.project_mapping

        if not testcases:
            raise exceptions.TestcaseNotFound

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

        return testcases