Ejemplo n.º 1
0
 def test_log_file(self):
     log_file_path = os.path.join(os.getcwd(), 'reports',
                                  "test_log_file.log")
     runner = apiautotest(failfast=True, log_file=log_file_path)
     runner.run(self.testcase_cli_path)
     self.assertTrue(os.path.isfile(log_file_path))
     os.remove(log_file_path)
Ejemplo n.º 2
0
 def test_testcase_simple_run_suite(self):
     testcase_path = "tests/testcases/setup.yml"
     tests_mapping = loader.load_tests(testcase_path)
     testcases = parser.parse_tests(tests_mapping)
     runner = apiautotest()
     test_suite = runner._add_tests(testcases)
     tests_results = runner._run_suite(test_suite)
     self.assertEqual(len(tests_results[0][1].records), 2)
Ejemplo n.º 3
0
 def test_html_report(self):
     report_save_dir = os.path.join(os.getcwd(), 'reports', "demo")
     runner = apiautotest(failfast=True, report_dir=report_save_dir)
     runner.run(self.testcase_cli_path)
     summary = runner.summary
     self.assertEqual(summary["stat"]["testcases"]["total"], 1)
     self.assertEqual(summary["stat"]["teststeps"]["total"], 10)
     self.assertEqual(summary["stat"]["teststeps"]["skipped"], 4)
     self.assertGreater(len(os.listdir(report_save_dir)), 0)
     shutil.rmtree(report_save_dir)
Ejemplo n.º 4
0
    def test_testsuite_add_tests(self):
        testcase_path = "tests/testsuites/create_users.yml"
        tests_mapping = loader.load_tests(testcase_path)

        testcases = parser.parse_tests(tests_mapping)
        runner = apiautotest()
        test_suite = runner._add_tests(testcases)

        self.assertEqual(len(test_suite._tests), 2)
        tests = test_suite._tests[0].teststeps
        self.assertIn("setup and reset all (override)",
                      tests[0]["config"]["name"].raw_string)
Ejemplo n.º 5
0
    def test_testcase_add_tests(self):
        testcase_path = "tests/testcases/setup.yml"
        tests_mapping = loader.load_tests(testcase_path)

        testcases = parser.parse_tests(tests_mapping)
        runner = apiautotest()
        test_suite = runner._add_tests(testcases)

        self.assertEqual(len(test_suite._tests), 1)
        teststeps = test_suite._tests[0].teststeps
        self.assertEqual(teststeps[0]["name"], "get token (setup)")
        self.assertEqual(teststeps[0]["variables"]["device_sn"],
                         "TESTCASE_SETUP_XXX")
        self.assertIn("api", teststeps[0])
Ejemplo n.º 6
0
    def test_testcase_complex_run_suite(self):
        testcase_path = "tests/testcases/create_user.yml"
        tests_mapping = loader.load_tests(testcase_path)
        testcases = parser.parse_tests(tests_mapping)
        runner = apiautotest()
        test_suite = runner._add_tests(testcases)
        tests_results = runner._run_suite(test_suite)
        self.assertEqual(len(tests_results[0][1].records), 2)

        results = tests_results[0][1]
        self.assertEqual(
            results.records[0]["name"],
            "setup and reset all (override) for TESTCASE_CREATE_XXX.")
        self.assertEqual(results.records[1]["name"],
                         "create user and check result.")
Ejemplo n.º 7
0
 def test_html_report_repsonse_image(self):
     report_save_dir = os.path.join(os.getcwd(), 'reports', "demo")
     runner = apiautotest(failfast=True, report_dir=report_save_dir)
     report = runner.run("tests/httpbin/load_image.yml")
     self.assertTrue(os.path.isfile(report))
     shutil.rmtree(report_save_dir)
Ejemplo n.º 8
0
 def setUp(self):
     self.testcase_cli_path = "tests/data/demo_testcase_cli.yml"
     self.testcase_file_path_list = [
         os.path.join(os.getcwd(), 'tests/data/demo_testcase_hardcode.yml'),
         os.path.join(os.getcwd(), 'tests/data/demo_testcase_hardcode.json')
     ]
     testcases = [{
         'config': {
             'name': 'testcase description',
             'request': {
                 'base_url': '',
                 'headers': {
                     'User-Agent': 'python-requests/2.18.4'
                 }
             },
             'variables': []
         },
         "teststeps": [{
             'name':
             '/api/get-token',
             'request': {
                 'url': 'http://127.0.0.1:5000/api/get-token',
                 'method': 'POST',
                 'headers': {
                     'Content-Type': 'application/json',
                     'app_version': '2.8.6',
                     'device_sn': 'FwgRiO7CNA50DSU',
                     'os_platform': 'ios',
                     'user_agent': 'iOS/10.3'
                 },
                 'json': {
                     'sign': '9c0c7e51c91ae963c833a4ccbab8d683c4a90c98'
                 }
             },
             'extract': [{
                 'token': 'content.token'
             }],
             'validate': [{
                 'eq': ['status_code', 200]
             }, {
                 'eq': ['headers.Content-Type', 'application/json']
             }, {
                 'eq': ['content.success', True]
             }]
         }, {
             'name':
             '/api/users/1000',
             'request': {
                 'url': 'http://127.0.0.1:5000/api/users/1000',
                 'method': 'POST',
                 'headers': {
                     'Content-Type': 'application/json',
                     'device_sn': 'FwgRiO7CNA50DSU',
                     'token': '$token'
                 },
                 'json': {
                     'name': 'user1',
                     'password': '******'
                 }
             },
             'validate': [{
                 'eq': ['status_code', 201]
             }, {
                 'eq': ['headers.Content-Type', 'application/json']
             }, {
                 'eq': ['content.success', True]
             }, {
                 'eq': ['content.msg', 'user created successfully.']
             }]
         }]
     }]
     self.tests_mapping = {"testcases": testcases}
     self.runner = apiautotest(failfast=True)
     self.reset_all()
Ejemplo n.º 9
0
def main_hrun():
    """ API test: parse command line options and run commands.
    """
    import argparse
    from apiautotest import logger
    from apiautotest.__about__ import __description__, __version__
    from apiautotest.api import apiautotest
    from apiautotest.compat import is_py2
    from apiautotest.validator import validate_json_file
    from apiautotest.utils import (create_scaffold, get_python2_retire_msg,
                                prettify_json_file)

    parser = argparse.ArgumentParser(description=__description__)
    parser.add_argument(
        '-V', '--version', dest='version', action='store_true',
        help="show version")
    parser.add_argument(
        'testcase_paths', nargs='*',
        help="testcase file path")
    parser.add_argument(
        '--log-level', default='INFO',
        help="Specify logging level, default is INFO.")
    parser.add_argument(
        '--log-file',
        help="Write logs to specified file path.")
    parser.add_argument(
        '--dot-env-path',
        help="Specify .env file path, which is useful for keeping sensitive data.")
    parser.add_argument(
        '--report-template',
        help="specify report template path.")
    parser.add_argument(
        '--report-dir',
        help="specify report save directory.")
    parser.add_argument(
        '--failfast', action='store_true', default=False,
        help="Stop the test run on the first error or failure.")
    parser.add_argument(
        '--save-tests', action='store_true', default=False,
        help="Save loaded tests and parsed tests to JSON file.")
    parser.add_argument(
        '--startproject',
        help="Specify new project name.")
    parser.add_argument(
        '--validate', nargs='*',
        help="Validate JSON testcase format.")
    parser.add_argument(
        '--prettify', nargs='*',
        help="Prettify JSON testcase format.")

    args = parser.parse_args()
    logger.setup_logger(args.log_level, args.log_file)

    if is_py2:
        logger.log_warning(get_python2_retire_msg())

    if args.version:
        logger.color_print("{}".format(__version__), "GREEN")
        exit(0)

    if args.validate:
        validate_json_file(args.validate)
        exit(0)
    if args.prettify:
        prettify_json_file(args.prettify)
        exit(0)

    project_name = args.startproject
    if project_name:
        create_scaffold(project_name)
        exit(0)

    runner = apiautotest(
        failfast=args.failfast,
        save_tests=args.save_tests,
        report_template=args.report_template,
        report_dir=args.report_dir
    )
    try:
        for path in args.testcase_paths:
            runner.run(path, dot_env_path=args.dot_env_path)
    except Exception:
        logger.log_error("!!!!!!!!!! exception stage: {} !!!!!!!!!!".format(runner.exception_stage))
        raise

    return 0