Skip to content

Commit

Permalink
Merge pull request #87 from eldaduzman/TASK/#84
Browse files Browse the repository at this point in the history
Task/#84
  • Loading branch information
eldaduzman authored Nov 6, 2022
2 parents ae6e26e + fe2c72d commit b8391ae
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 1 deletion.
55 changes: 55 additions & 0 deletions src/pymeter/api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,42 @@ def tearDownClass(cls):
example - 4:
--------------
We can create vars from with in JMeters context using the `Vars` class
.. code-block:: python
from pymeter.api.config import TestPlan, ThreadGroupSimple, Vars
from pymeter.api.samplers import HttpSampler
from pymeter.api.timers import ConstantTimer
from pymeter.api.reporters import HtmlReporter
jmeter_variables = Vars(id1="value1", id2="value2")
html_reporter = HtmlReporter()
timer = ConstantTimer(2000)
http_sampler1 = HttpSampler(
"Echo_${id1}", "https://postman-echo.com/get?var=${id1}", timer
)
thread_group1 = ThreadGroupSimple(3, 1)
thread_group1.children(http_sampler1)
http_sampler2 = HttpSampler("Echo_${id2}", "https://postman-echo.com/get?var=do", timer)
thread_group2 = ThreadGroupSimple(3, 1, http_sampler2)
test_plan = TestPlan(thread_group1, thread_group2, html_reporter, jmeter_variables)
stats = test_plan.run()
We can also set a single variable using the `set` method
.. code-block:: python
from pymeter.api.config import Vars
jmeter_variables = Vars(id1="value1", id2="value2")
jmeter_variables.set("id1", "v2")
example - 5:
--------------
We Can also generate data for each thread group:
.. code-block:: python
from pymeter.api.config import TestPlan, ThreadGroupSimple, CsvDataset
from pymeter.api.samplers import HttpSampler
from pymeter.api.timers import ConstantTimer
Expand Down Expand Up @@ -144,6 +178,27 @@ class BaseConfigElement(TestPlanChildElement):
"""base class for all config elements"""


class Vars(TestPlanChildElement):
"""Vars are key value pairs"""

def __init__(self, **variables) -> None:
self._vars_instance = TestPlanChildElement.jmeter_class.vars()
for key, value in variables.items():
self.set(key, value)
super().__init__()

def children(self, *children):
raise ChildrenAreNotAllowed("Cant append children to vars")

def set(self, key: str, value: str):
"""Sets a single key value pair"""
if not isinstance(key, str):
raise TypeError("Keys must be strings")

self._vars_instance.set(key, str(value))
return self


class CsvDataset(TestPlanChildElement, ThreadGroupChildElement):
"""
csv data set allows you to append unique data set to samplers
Expand Down
2 changes: 1 addition & 1 deletion utests/test_csv_data_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class TestCsvDataSet(TestCase):

def test_csv_data_set_children(self):
with self.assertRaises(ChildrenAreNotAllowed) as exp:
CsvDataset("utests/test_csv_data_set.py").children()
CsvDataset(CSV_FILE_PATH).children()
self.assertEqual(
str(exp.exception),
"Cant append children to a csv_data_set",
Expand Down
131 changes: 131 additions & 0 deletions utests/test_vars.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
"""unittest module"""
import os
import uuid
from unittest import TestCase, main
from collections import Counter
from pymeter.api import ChildrenAreNotAllowed
from pymeter.api.config import (
TestPlan,
ThreadGroupSimple,
Vars,
)
from pymeter.api.reporters import HtmlReporter
from pymeter.api.samplers import HttpSampler


class TestVars(TestCase):
"""Testing vars"""

def test_vars_children(self):
with self.assertRaises(ChildrenAreNotAllowed) as exp:
Vars().children()
self.assertEqual(
str(exp.exception),
"Cant append children to vars",
)

def test_vars_set_from_constructor(self):

output_dir = os.path.join("output", str(uuid.uuid4()))
html_reporter = HtmlReporter(output_dir)
variables = Vars(my_id="value1")
http_sampler1 = HttpSampler(
"Echo_${my_id}", "https://postman-echo.com/get?var=${my_id}"
)
thread_group = ThreadGroupSimple(2, 1)
thread_group.children(http_sampler1)

test_plan = TestPlan(thread_group, html_reporter, variables)
test_plan.run()

self.assertTrue(os.path.exists(output_dir))
path_to_jtl = os.path.join(output_dir, "report.jtl")
with open(path_to_jtl, "r", encoding="utf-8") as jtl_file:
next(jtl_file)
all_samplers = [line.split(",")[2] for line in jtl_file]
cntr = Counter(all_samplers)
self.assertDictEqual({"Echo_value1": 2}, cntr)

def test_vars_set_from_set_method(self):

output_dir = os.path.join("output", str(uuid.uuid4()))
html_reporter = HtmlReporter(output_dir)
variables = Vars()
variables.set("my_id", "value1")
http_sampler1 = HttpSampler(
"Echo_${my_id}", "https://postman-echo.com/get?var=${my_id}"
)
thread_group = ThreadGroupSimple(2, 1)
thread_group.children(http_sampler1)

test_plan = TestPlan(thread_group, html_reporter, variables)
test_plan.run()

self.assertTrue(os.path.exists(output_dir))
path_to_jtl = os.path.join(output_dir, "report.jtl")
with open(path_to_jtl, "r", encoding="utf-8") as jtl_file:
next(jtl_file)
all_samplers = [line.split(",")[2] for line in jtl_file]
cntr = Counter(all_samplers)
self.assertDictEqual({"Echo_value1": 2}, cntr)

def test_vars_value_is_int(self):

output_dir = os.path.join("output", str(uuid.uuid4()))
html_reporter = HtmlReporter(output_dir)
variables = Vars()
variables.set("my_id", 1)
http_sampler1 = HttpSampler(
"Echo_${my_id}", "https://postman-echo.com/get?var=${my_id}"
)
thread_group = ThreadGroupSimple(2, 1)
thread_group.children(http_sampler1)

test_plan = TestPlan(thread_group, html_reporter, variables)
test_plan.run()

self.assertTrue(os.path.exists(output_dir))
path_to_jtl = os.path.join(output_dir, "report.jtl")
with open(path_to_jtl, "r", encoding="utf-8") as jtl_file:
next(jtl_file)
all_samplers = [line.split(",")[2] for line in jtl_file]
cntr = Counter(all_samplers)
self.assertDictEqual({"Echo_1": 2}, cntr)

def test_vars_value_is_stringable_class(self):
class C:
def __repr__(self) -> str:
return "hello"

output_dir = os.path.join("output", str(uuid.uuid4()))
html_reporter = HtmlReporter(output_dir)
variables = Vars()
variables.set("my_id", C())
http_sampler1 = HttpSampler(
"Echo_${my_id}", "https://postman-echo.com/get?var=${my_id}"
)
thread_group = ThreadGroupSimple(2, 1)
thread_group.children(http_sampler1)

test_plan = TestPlan(thread_group, html_reporter, variables)
test_plan.run()

self.assertTrue(os.path.exists(output_dir))
path_to_jtl = os.path.join(output_dir, "report.jtl")
with open(path_to_jtl, "r", encoding="utf-8") as jtl_file:
next(jtl_file)
all_samplers = [line.split(",")[2] for line in jtl_file]
cntr = Counter(all_samplers)
self.assertDictEqual({"Echo_hello": 2}, cntr)

def test_vars_illegal_key_type(self):

with self.assertRaises(TypeError) as exp:
variables = Vars()
variables.set(1, "value1")

self.assertEqual(str(exp.exception), "Keys must be strings")


if __name__ == "__main__":
main()

0 comments on commit b8391ae

Please sign in to comment.