Best Python code snippet using autotest_python
tests.py
Source:tests.py
...6 report_ids = [employee['Employee ID'] for employee in report]7 full_report_ids = [employee['Employee ID']8 for employee in full_report if employee[field_to_check] == data_to_check]9 return report_ids == full_report_ids10def test_validation(report, validation_function, field_to_check, max_length, is_mandatory):11 is_valid = True12 for employee in report:13 if not validation_function(employee[field_to_check], max_length, is_mandatory):14 is_valid = False15 return is_valid16def all_relevant_employees_present(report, full_report, field_to_check, data_to_check):17 all_employees_in_report = len(report)18 all_employees_from_location = len([19 employee for employee in full_report if employee[field_to_check] == data_to_check])20 return all_employees_in_report == all_employees_from_location21class TestLocationReport(unittest.TestCase):22 location = "Cardiff"23 good_employee_data = create_emp_data_set(50, True)24 bad_employee_data = create_emp_data_set(100, False)25 good_location_report = generate_location_report(26 good_employee_data, location)27 bad_location_report = generate_location_report(28 bad_employee_data, location)29 good_full_report = generate_full_report(good_employee_data)30 bad_full_report = generate_full_report(bad_employee_data)31 def test_all_employees_are_from_location(self):32 self.assertTrue(check_all_employees_match_report_criteria(33 self.good_location_report, self.good_full_report, 'Location', self.location))34 self.assertTrue(check_all_employees_match_report_criteria(35 self.bad_location_report, self.bad_full_report, 'Location', self.location))36 def test_all_employees_from_location_are_in_report(self):37 self.assertTrue(all_relevant_employees_present(38 self.good_location_report, self.good_full_report, "Location", self.location))39 self.assertTrue(all_relevant_employees_present(40 self.bad_location_report, self.bad_full_report, "Location", self.location))41 def test_employee_id_validation(self):42 self.assertTrue(test_validation(self.good_location_report,43 is_number_valid, "Employee ID", 10, True))44 self.assertTrue(test_validation(self.bad_location_report,45 is_number_valid, "Employee ID", 10, True))46 def test_employee_name_validation(self):47 self.assertTrue(test_validation(self.good_location_report,48 is_character_valid, "Employee Name", 50, True))49 self.assertTrue(test_validation(self.bad_location_report,50 is_character_valid, "Employee Name", 50, True))51 def test_job_title_validation(self):52 self.assertTrue(test_validation(self.good_location_report,53 is_character_valid, "Job Title", 50, True))54 self.assertTrue(test_validation(self.bad_location_report,55 is_character_valid, "Job Title", 50, True))56 def test_salary_validation(self):57 self.assertTrue(test_validation(self.good_location_report,58 is_number_valid, "Salary", 10, True))59 self.assertTrue(test_validation(self.bad_location_report,60 is_number_valid, "Salary", 10, True))61 def test_department_name_validation(self):62 self.assertTrue(test_validation(self.good_location_report,63 is_character_valid, "Department Name", 50, True))64 self.assertTrue(test_validation(self.bad_location_report,65 is_character_valid, "Department Name", 50, True))66class TestDepartmentReport(unittest.TestCase):67 department_id = 468 good_employee_data = create_emp_data_set(50, True)69 bad_employee_data = create_emp_data_set(100, False)70 good_department_report = generate_department_report(71 good_employee_data, department_id)72 bad_department_report = generate_department_report(73 bad_employee_data, department_id)74 good_full_report = generate_full_report(good_employee_data)75 bad_full_report = generate_full_report(bad_employee_data)76 def test_all_employees_are_from_department(self):77 self.assertTrue(check_all_employees_match_report_criteria(78 self.good_department_report, self.good_full_report, 'Department ID', self.department_id))79 self.assertTrue(check_all_employees_match_report_criteria(80 self.bad_department_report, self.bad_full_report, 'Department ID', self.department_id))81 def test_all_employees_from_department_are_in_report(self):82 self.assertTrue(all_relevant_employees_present(83 self.good_department_report, self.good_full_report, "Department ID", self.department_id))84 self.assertTrue(all_relevant_employees_present(85 self.bad_department_report, self.bad_full_report, "Department ID", self.department_id))86 def test_employee_id_validation(self):87 self.assertTrue(test_validation(self.good_department_report,88 is_number_valid, "Employee ID", 10, True))89 self.assertTrue(test_validation(self.bad_department_report,90 is_number_valid, "Employee ID", 10, True))91 def test_employee_name_validation(self):92 self.assertTrue(test_validation(self.good_department_report,93 is_character_valid, "Employee Name", 50, True))94 self.assertTrue(test_validation(self.bad_department_report,95 is_character_valid, "Employee Name", 50, True))96 def test_job_title_validation(self):97 self.assertTrue(test_validation(self.good_department_report,98 is_character_valid, "Job Title", 50, True))99 self.assertTrue(test_validation(self.bad_department_report,100 is_character_valid, "Job Title", 50, True))101 def test_salary_validation(self):102 self.assertTrue(test_validation(self.good_department_report,103 is_number_valid, "Salary", 10, True))104 self.assertTrue(test_validation(self.bad_department_report,105 is_number_valid, "Salary", 10, True))106if __name__ == '__main__':...
test_services.py
Source:test_services.py
1from pytest import fixture2from models import Services3from Modules.services import validate_service, translate_gender, services_data_to_dict, \4 services_to_string, services_total_duration, services_total_price5@fixture(scope="function")6def generate_test_service_dict():7 return {"name": "Test_service", "price": 121, "gender": "MALE", "service_duration": 120}8@fixture(scope="function")9def generate_test_services_list(generate_test_service_dict):10 return [generate_test_service_dict, generate_test_service_dict, generate_test_service_dict]11@fixture(scope="function")12def generate_test_service_object():13 return Services(14 id=1,15 name="Test",16 price=15,17 description="Description",18 created_at="2020-04-05 16:00",19 gender="MALE",20 service_duration=12021 )22def test_validate_service_does_not_allow_too_long_name(generate_test_service_dict):23 # GIVEN24 test_service = generate_test_service_dict25 test_service["name"] = "TestTestTestTestTestTestTestTestTest"26 # WHEN27 test_validation = validate_service(test_service)28 # THEN29 assert test_validation["success"] is False30 assert test_validation["error"] == "Nazwa uslugi jest za dluga"31def test_validate_service_does_not_allow_negative_price(generate_test_service_dict):32 # GIVEN33 test_service = generate_test_service_dict34 test_service["price"] = -535 # WHEN36 test_validation = validate_service(test_service)37 # THEN38 assert test_validation["success"] is False39 assert test_validation["error"] == "Cena nie moze byc ujemna"40def test_validate_service_does_not_allow_wrong_gender(generate_test_service_dict):41 # GIVEN42 test_service = generate_test_service_dict43 test_service["gender"] = "NoneOfTheAbove"44 # WHEN45 test_validation = validate_service(test_service)46 # THEN47 assert test_validation["success"] is False48 assert test_validation["error"] == "Niepoprawna plec"49def test_validate_service_does_not_allow_negative_service_duration(generate_test_service_dict):50 # GIVEN51 test_service = generate_test_service_dict52 test_service["service_duration"] = -10053 # WHEN54 test_validation = validate_service(test_service)55 # THEN56 assert test_validation["success"] is False57 assert test_validation["error"] == "Czas trwania uslugi musi byc dodatni"58def test_translate_gender_translates_male_correctly():59 # GIVEN60 test_string1 = "male"61 test_string2 = "MALE"62 test_string3 = "Male"63 # WHEN64 test_result1 = translate_gender(test_string1)65 test_result2 = translate_gender(test_string2)66 test_result3 = translate_gender(test_string3)67 # THEN68 assert test_result1 == "M"69 assert test_result2 == "M"70 assert test_result3 == "M"71def test_translate_gender_translates_female_correctly():72 # GIVEN73 test_string1 = "female"74 # WHEN75 test_result1 = translate_gender(test_string1)76 # THEN77 assert test_result1 == "K"78def test_services_data_to_dict_has_proper_structure(generate_test_service_object):79 # GIVEN80 test_service = generate_test_service_object81 # WHEN82 test_result = services_data_to_dict(test_service)83 # THEN84 assert "id" in test_result85 assert "description" in test_result86 assert "gender" in test_result87 assert "name" in test_result88 assert "price" in test_result89 assert "service_duration" in test_result90 assert "created_at" not in test_result91def test_services_to_string_converts_correctly(generate_test_services_list):92 # GIVEN93 test_services_list = generate_test_services_list94 # WHEN95 test_result = services_to_string(test_services_list)96 # THEN97 assert test_result == "Test_service, Test_service, Test_service"98def test_services_total_duration_is_calculated_correctly(generate_test_services_list):99 # GIVEN100 test_services_list = generate_test_services_list101 # WHEN102 test_result = services_total_duration(test_services_list)103 # THEN104 assert test_result == "360 min"105def test_services_total_price_is_calculated_correctly(generate_test_services_list):106 # GIVEN107 test_services_list = generate_test_services_list108 # WHEN109 test_result = services_total_price(test_services_list)110 # THEN...
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!!