Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use of decorators for more idiomatic experience #83

Open
Minitour opened this issue Oct 30, 2022 · 2 comments
Open

Use of decorators for more idiomatic experience #83

Minitour opened this issue Oct 30, 2022 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@Minitour
Copy link
Contributor

Minitour commented Oct 30, 2022

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)

@eldaduzman
Copy link
Owner

Hi, this looks interesting, however I suspect that it might be too case-specific to be integrated into the project, I'd love to get more opinions on that one.

@Minitour
Copy link
Contributor Author

What I posted in here is indeed case-specific. But perhaps what I wanted to suggest is in addition to the classes there could also be decorators.

Consider:

@http_sampler(url='http://helloworld')
@thread_group(threads=10, duration=60)
@html_reporter(directory='output')
def my_test(test_plan):
   stats = test_plan.run()
   # make assertions

Some logic would determine the hierarchy among the decorators and build a test plan out of them.
Again it is just an idea, but might be more developer friendly.

@eldaduzman eldaduzman added the enhancement New feature or request label Oct 31, 2022
@eldaduzman eldaduzman self-assigned this Oct 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants