Skip to content

Use of decorators for more idiomatic experience  #83

Description

@Minitour

This is more of a suggestion rather than an issue.

This is how I am defining my custom decorators so I can use them in my unit tests:

@inject
def http_get_performance_test(url: str,
                              threads=10,
                              duration=60,
                              html_reporter=Provide[Container.html_report]): # injected from dependency injection for a unified report
    def decorator(fun):
        def wrapper(*args, **kwargs):
            timer = UniformRandomTimer(1000, 2000)
            http_sampler = HttpSampler(
                fun.__name__,
                url,
                timer
            )
            tg = ThreadGroupWithRampUpAndHold(
                threads, 1, duration, http_sampler, name="Some Name"
            )
            tp = TestPlan(tg, html_reporter)
            fun(*args, **kwargs, test_plan=tp)

        return wrapper

    return decorator

When in use the unit tests look a lot cleaner:

class TestTestPlanClass(TestCase):
    @e2e.utils.decorators.http_get_performance_test(
        url="https://postman-echo.com/get?var=${__Random(0,10)}",
        threads=10,
        duration=30
    )
    def test_2(self, test_plan):
        stats = test_plan.run()
        self.assertLess(stats.sample_time_99_percentile_milliseconds, 2000)

I believe you can achieve a much nicer Developer Experience with decorators (you can also chain them)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions