Best Python code snippet using autotest_python
run_test.py
Source:run_test.py  
1#!/usr/bin/env python32#      This file is part of quicky_tools3#      Copyright (C) 2020  Julien Thevenon ( julien_thevenon at yahoo.fr )4#5#      This program is free software: you can redistribute it and/or modify6#      it under the terms of the GNU General Public License as published by7#      the Free Software Foundation, either version 3 of the License, or8#      (at your option) any later version.9#10#      This program is distributed in the hope that it will be useful,11#      but WITHOUT ANY WARRANTY; without even the implied warranty of12#      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the13#      GNU General Public License for more details.14#15#      You should have received a copy of the GNU General Public License16#      along with this program.  If not, see <http://www.gnu.org/licenses/>17import shlex18import shutil19import subprocess20import sys21import os22import glob23from os import path24class TestInfo:25    def __init__(self):26        self._args = ""27        self._exe = ""28        self._expected_status = 029        self._timeout = 530        self._reference_files = []31        self._expected_stdout_strings = []32    def set_arg(self, arg):33        self._args = arg34    def get_arg(self):35        return self._args36    def set_exe(self, exe):37        self._exe = exe38    def get_exe(self):39        return self._exe40    def set_expected_status(self, status):41        self._expected_status = int(status)42    def get_expected_status(self):43        return self._expected_status44    def set_timeout(self, timeout):45        self._timeout = int(timeout)46    def get_timeout(self):47        return self._timeout48    def add_reference_file(self, file):49        self._reference_files.append(file)50    def get_reference_files(self):51        return self._reference_files52    def add_expected_stdout_string(self, string):53        self._expected_stdout_strings.append(string)54    def get_expected_stdout_strings(self):55        return self._expected_stdout_strings56def extract_test_info(test_path):57    info_file_name = path.join(test_path, "test.info")58    if not path.exists(info_file_name):59        print("ERROR : Missing test.info for test in " + test_path)60        return [-1, 0]61    print("--> Parse file " + info_file_name)62    test_info = TestInfo()63    with open(info_file_name, "r") as info_file:64        line_number = 165        for line in info_file:66            line = line.rstrip()67            # Ignore comments68            if line.startswith("#"):69                continue70            # Extract key values71            if -1 != line.find(":"):72                (key, value) = line.split(":", maxsplit=1)73                if "exe_file" == key:74                    test_info.set_exe(value)75                elif "args" == key:76                    while -1 != value.find("<test_location>"):77                        value = value.replace("<test_location>", test_path)78                    test_info.set_arg(value)79                elif "expected_stdout_string" == key:80                    test_info.add_expected_stdout_string(value)81                elif "expected_status" == key:82                    test_info.set_expected_status(value)83                elif "timeout" == key:84                    test_info.set_timeout(value)85                else:86                    print("ERROR: Unknown key " + key + ' in line "' + line + '" ', end='')87                    print("at " + path.basename(info_file_name) + ":" + str(line_number))88                    return [-1, 0]89            else:90                print('ERROR: No key/value syntax in line "' + line, end='')91                print('" at ' + path.basename(info_file_name) + ":" + str(line_number))92                return [-1, 0]93            line_number += 194    if "" == test_info.get_exe():95        print('ERROR: Exe file not defined')96        return [-1, 0]97    return [0, test_info]98def perform_test(name, test_path, compilation_path):99    print('-> Prepare test "' + name + '"')100    (status, test_info) = extract_test_info(test_path)101    # Check if extraction of test information was OK102    if 0 != status:103        return status104    # Find executable file ( different naming between quicky tools and CMake105    binary_candidates = (path.join(compilation_path, test_info.get_exe()),106                         path.join(compilation_path, "bin", test_info.get_exe() + ".exe"))107    exe_name = ""108    for binary_candidate in binary_candidates:109        if path.exists(binary_candidate):110            exe_name = binary_candidate111    if "" == exe_name:112        print("ERROR : Unable to find exe " + test_info.get_exe() + " in " + compilation_path)113        return -1114    # Build the command115    test_cmd = [exe_name]116    test_cmd += shlex.split(test_info.get_arg())117    # Log the command118    with open("cmd.log", "w") as cmd_file:119        cmd_file.write(' '.join(test_cmd))120    # Launch process121    print('-> Run test "' + name + '"')122    process = subprocess.Popen(test_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)123    try:124        stdouts, stderrs = process.communicate(timeout=test_info.get_timeout())125    except subprocess.TimeoutExpired:126        process.kill()127        stdouts, stderrs = process.communicate()128    # Compare expected status vs status129    if process.returncode != test_info.get_expected_status():130        print("ERROR: return status(" + str(process.returncode) + ") different from expected one(", end='')131        print(str(test_info.get_expected_status()) + ")")132        status = -1133    # Dump logs for stdout, stderr, return status134    with open("stdout.log", "w") as stdout_log:135        stdout_log.write(str(stdouts, 'utf-8'))136    with open("stderr.log", "w") as stderr_log:137        stderr_log.write(str(stderrs, 'utf-8'))138    with open("status.log", "w") as status_log:139        status_log.write(str(process.returncode))140    # Check reference files141    root_reference = path.join(test_path, "references")142    for abs_reference_file in glob.iglob(path.join(root_reference, "**"), recursive=True):143        if path.isdir(abs_reference_file) or not path.exists(abs_reference_file):144            continue145        relative_file = path.basename(abs_reference_file)146        reference_file = path.dirname(abs_reference_file)147        while reference_file != root_reference:148            relative_file = path.join(path.basename(reference_file), relative_file)149            reference_file = path.dirname(reference_file)150        if path.exists(relative_file):151            print("--> Compare " + relative_file + " with reference")152            with subprocess.Popen(["diff", relative_file, abs_reference_file],153                                  stderr=subprocess.PIPE, stdout=subprocess.PIPE) as diff_process:154                diff_process.wait()155                if 0 != diff_process.returncode:156                    print("ERROR: " + ' '.join(diff_process.args))157                    status = diff_process.returncode158        else:159            print('ERROR: file "' + relative_file + '" expected but not created')160            status = -1161    # Search for expected strings162    stdout_content = str(stdouts, 'utf-8').split('\n')163    expected_strings = test_info.get_expected_stdout_strings()164    for line in stdout_content:165        if len(expected_strings):166            if line == expected_strings[0]:167                expected_strings.pop(0)168        else:169            break170    for expected_stdout_string in expected_strings:171        print('ERROR: expected string "' + expected_stdout_string + '" is missing in stdout ', end='')172        print('or was not at the expected place')173        status = -1174    return status175def main(argv):176    if 1 != len(argv):177        print("ERROR : need to have 1 argument")178        sys.exit(-1)179    assert ("PWD" in os.environ)180    compilation_path = os.environ["PWD"]181    print("Compilation path : " + compilation_path)182    # Get test root location183    test_root = argv[0]184    print("Run tests located in " + test_root)185    if not path.isabs(test_root):186        test_root = path.join(compilation_path, test_root)187    # Check test location existence188    if not path.exists(test_root):189        print("ERROR : directory " + test_root + " do not exists")190        sys.exit(-1)191    if not path.isdir(test_root):192        print("ERROR : " + test_root + " is not a directory")193        sys.exit(-1)194    # Create file tree for test execution195    execution_path = path.join(compilation_path, "test_results")196    print("Execution path : " + execution_path)197    if not path.exists(execution_path):198        os.mkdir(execution_path)199    test_status = 0200    # Iterate over tests201    for item in os.scandir(test_root):202        if item.is_dir():203            test_location = path.join(test_root, item.name)204            test_execution_path = path.join(execution_path, item.name)205            # Remove test execution directory if it already exist206            if path.exists(test_execution_path):207                shutil.rmtree(test_execution_path)208            # Create test execution directory209            os.mkdir(test_execution_path)210            # Go in specific test execution directory211            os.chdir(test_execution_path)212            test_status += perform_test(item.name, test_location, compilation_path)213            # Come back to execution directory after the test214            os.chdir(execution_path)215    if 0 == test_status:216        print("TESTS SUCCESSFUL")217    else:218        print("TESTS FAILED")219    sys.exit(test_status)220if __name__ == "__main__":221    # execute only if run as a script222    main(sys.argv[1:])...models_test.py
Source:models_test.py  
...12        self._frontend_common_teardown()13    def _create_task(self):14        return models.SpecialTask.objects.create(15                host=self.hosts[0], task=models.SpecialTask.Task.VERIFY)16    def test_execution_path(self):17        task = self._create_task()18        self.assertEquals(task.execution_path(), 'hosts/host1/1-verify')19    def test_status(self):20        task = self._create_task()21        self.assertEquals(task.status, 'Queued')22        task.update_object(is_active=True)23        self.assertEquals(task.status, 'Running')24        task.update_object(is_active=False, is_complete=True)25        self.assertEquals(task.status, 'Completed')26    def test_activate(self):27        task = self._create_task()28        task.activate()29        self.assertTrue(task.is_active)30        self.assertFalse(task.is_complete)...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
