Best Python code snippet using locust
test_env.py
Source:test_env.py
1from locust import (2 constant,3)4from locust.env import Environment5from locust.user import (6 User,7 task,8)9from locust.user.task import TaskSet10from .testcases import LocustTestCase11from .fake_module1_for_env_test import MyUserWithSameName as MyUserWithSameName112from .fake_module2_for_env_test import MyUserWithSameName as MyUserWithSameName213class TestEnvironment(LocustTestCase):14 def test_user_classes_count(self):15 class MyUser1(User):16 wait_time = constant(0)17 @task18 def my_task(self):19 pass20 class MyUser2(User):21 wait_time = constant(0)22 @task23 def my_task(self):24 pass25 environment = Environment(user_classes=[MyUser1, MyUser2])26 self.assertDictEqual({"MyUser1": MyUser1, "MyUser2": MyUser2}, environment.user_classes_by_name)27 def test_user_classes_with_same_name_is_error(self):28 with self.assertRaises(ValueError) as e:29 environment = Environment(user_classes=[MyUserWithSameName1, MyUserWithSameName2])30 self.assertEqual(31 e.exception.args[0],32 "The following user classes have the same class name: locust.test.fake_module1_for_env_test.MyUserWithSameName, locust.test.fake_module2_for_env_test.MyUserWithSameName",33 )34 def test_assign_equal_weights(self):35 def verify_tasks(u, target_tasks):36 self.assertEqual(len(u.tasks), len(target_tasks))37 tasks = [t.__name__ for t in u.tasks]38 self.assertEqual(len(tasks), len(set(tasks)))39 self.assertEqual(set(tasks), set(target_tasks))40 # Base case41 class MyUser1(User):42 wait_time = constant(0)43 @task(4)44 def my_task(self):45 pass46 @task(1)47 def my_task_2(self):48 pass49 environment = Environment(user_classes=[MyUser1])50 environment.assign_equal_weights()51 u = environment.user_classes[0]52 verify_tasks(u, ["my_task", "my_task_2"])53 # Testing nested task sets54 class MyUser2(User):55 @task56 class TopLevelTaskSet(TaskSet):57 @task58 class IndexTaskSet(TaskSet):59 @task(10)60 def index(self):61 self.client.get("/")62 @task63 def stop(self):64 self.client.get("/hi")65 @task(2)66 def stats(self):67 self.client.get("/stats/requests")68 environment = Environment(user_classes=[MyUser2])69 environment.assign_equal_weights()70 u = environment.user_classes[0]71 verify_tasks(u, ["index", "stop", "stats"])72 # Testing task assignment via instance variable73 def outside_task():74 pass75 def outside_task_2():76 pass77 class SingleTaskSet(TaskSet):78 tasks = [outside_task, outside_task, outside_task_2]79 class MyUser3(User):80 tasks = [SingleTaskSet, outside_task]81 environment = Environment(user_classes=[MyUser3])82 environment.assign_equal_weights()83 u = environment.user_classes[0]84 verify_tasks(u, ["outside_task", "outside_task_2"])85 # Testing task assignment via dict86 class DictTaskSet(TaskSet):87 def dict_task_1():88 pass89 def dict_task_2():90 pass91 def dict_task_3():92 pass93 tasks = {94 dict_task_1: 5,95 dict_task_2: 3,96 dict_task_3: 1,97 }98 class MyUser4(User):99 tasks = [DictTaskSet, SingleTaskSet, SingleTaskSet]100 # Assign user tasks in dict101 environment = Environment(user_classes=[MyUser4])102 environment.assign_equal_weights()103 u = environment.user_classes[0]104 verify_tasks(u, ["outside_task", "outside_task_2", "dict_task_1", "dict_task_2", "dict_task_3"])105 class MyUser5(User):106 tasks = {107 DictTaskSet: 5,108 SingleTaskSet: 3,109 outside_task: 6,110 }111 environment = Environment(user_classes=[MyUser5])112 environment.assign_equal_weights()113 u = environment.user_classes[0]...
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!!