@@ -759,7 +759,7 @@ def tick(self):
759
759
response = requests .get ("http://127.0.0.1:%i/stop" % self .web_port )
760
760
self .assertEqual (response .json ()["message" ], "Test stopped" )
761
761
762
- def test_swarm_custom_argument (self ):
762
+ def test_swarm_custom_argument_without_default_value (self ):
763
763
my_dict = {}
764
764
765
765
class MyUser (User ):
@@ -771,10 +771,10 @@ def my_task(self):
771
771
my_dict ["val" ] = self .environment .parsed_options .my_argument
772
772
773
773
@locust .events .init_command_line_parser .add_listener
774
- def _ (parser , ** kw ):
774
+ def _ (parser ):
775
775
parser .add_argument ("--my-argument" , type = int , help = "Give me a number" )
776
776
777
- parsed_options = parse_options (args = [ "--my-argument" , "42" ] )
777
+ parsed_options = parse_options ()
778
778
self .environment .user_classes = [MyUser ]
779
779
self .environment .parsed_options = parsed_options
780
780
self .environment .web_ui .parsed_options = parsed_options
@@ -783,7 +783,59 @@ def _(parser, **kw):
783
783
data = {"user_count" : 1 , "spawn_rate" : 1 , "host" : "" , "my_argument" : "42" },
784
784
)
785
785
self .assertEqual (200 , response .status_code )
786
- self .assertEqual (my_dict ["val" ], 42 )
786
+ self .assertEqual ("42" , my_dict ["val" ])
787
+
788
+ def test_swarm_custom_argument_with_default_value (self ):
789
+ my_dict = {}
790
+
791
+ class MyUser (User ):
792
+ host = "http://example.com"
793
+ wait_time = constant (1 )
794
+
795
+ @task (1 )
796
+ def my_task (self ):
797
+ my_dict ["val" ] = self .environment .parsed_options .my_argument
798
+
799
+ @locust .events .init_command_line_parser .add_listener
800
+ def _ (parser ):
801
+ parser .add_argument ("--my-argument" , type = int , help = "Give me a number" , default = 24 )
802
+
803
+ parsed_options = parse_options ()
804
+ self .environment .user_classes = [MyUser ]
805
+ self .environment .parsed_options = parsed_options
806
+ self .environment .web_ui .parsed_options = parsed_options
807
+ response = requests .post (
808
+ "http://127.0.0.1:%i/swarm" % self .web_port ,
809
+ data = {"user_count" : 1 , "spawn_rate" : 1 , "host" : "" , "my_argument" : "42" },
810
+ )
811
+ self .assertEqual (200 , response .status_code )
812
+ self .assertEqual (42 , my_dict ["val" ])
813
+
814
+ def test_swarm_override_command_line_argument (self ):
815
+ my_dict = {}
816
+
817
+ class MyUser (User ):
818
+ host = "http://example.com"
819
+ wait_time = constant (1 )
820
+
821
+ @task (1 )
822
+ def my_task (self ):
823
+ my_dict ["val" ] = self .environment .parsed_options .my_argument
824
+
825
+ @locust .events .init_command_line_parser .add_listener
826
+ def _ (parser ):
827
+ parser .add_argument ("--my-argument" , type = int , help = "Give me a number" )
828
+
829
+ parsed_options = parse_options (args = ["--my-argument" , "24" ])
830
+ self .environment .user_classes = [MyUser ]
831
+ self .environment .parsed_options = parsed_options
832
+ self .environment .web_ui .parsed_options = parsed_options
833
+ response = requests .post (
834
+ "http://127.0.0.1:%i/swarm" % self .web_port ,
835
+ data = {"user_count" : 1 , "spawn_rate" : 1 , "host" : "" , "my_argument" : "42" },
836
+ )
837
+ self .assertEqual (200 , response .status_code )
838
+ self .assertEqual (42 , my_dict ["val" ])
787
839
788
840
def test_swarm_host_value_not_specified (self ):
789
841
class MyUser (User ):
0 commit comments