-
Notifications
You must be signed in to change notification settings - Fork 3k
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 pytest tests as locust tasks #955
Comments
sorry, no such instructions exist. please open a PR if you create some. |
Can someone provide more information? I need this too - i don't want to write each test twice - in locust and pytest+allure. |
This would be a great add-on, we have all our tests written in pytest and will be great if they can be consumed within locust tasks |
+1 for this |
+1 |
If someone were to build this (implemented as some special User class that discovers and registers test cases as tasks?), it would be very welcome (maybe as part of locust-plugins though) |
This is a great feature. |
This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 10 days. |
This issue was closed because it has been stalled for 10 days with no activity. This does not necessarily mean that the issue is bad, but it most likely means that nobody is willing to take the time to fix it. If you have found Locust useful, then consider contributing a fix yourself! |
I am trying to integrate pytest and locust and facing the issue PytestCollectionWarning: cannot collect test class because it has a init constructor |
@prestomation can you check this issue, and share a solution for this. @divakar260183 please share the repo if possible |
@manishaery is there any solution on "This would be a great add-on, we have all our tests written in pytest and will be great if they can be consumed within locust tasks" |
@prestomation The repo is posted at https://github.com/divakar260183/APIAutomation.git |
I think it is great that you are exploring this. I dont think that it is easy to do though. I think you would need to hook in to pytest on a slightly lower level. The "cannot collect test class because it has a init constructor" is because pytest expects test classes to have a default constructor, but Locust Users (and TaskSets) require parameters (a reference to the Environment instance). I dont see an obvious solution to this so you'll need to to a bit of digging. Or maybe you could use this approach (calling pytest from inside a regular User class): https://docs.pytest.org/en/6.2.x/usage.html#calling-pytest-from-python-code |
@cyberw I have already done this in my file where I am collecting all my test as a Locust task import pytest class TestCollector:
test_collector = TestCollector() pytest.main(['LocustTest', '--collect-only'], plugins=[test_collector]) class TestTaskSet(TaskSet):
class WebsiteUser(HttpUser): When executing this using locust -f main.py, I am getting the error, "PytestCollectionWarning: cannot collect test class because it has a init constructor" My Test Class is : import json import pytest request_url = ""
|
I can only repeat what I said: pytest test classes (TestShifts in your case) are not allowed to have a constructor (because pytest will instantiate the classes from within its framework, and it has no idea what parameters to pass) and User/TaskSet has (and needs) a parametrized constructor. I dont know the best way to resolve this. |
I think the most straight forward way would be to define the User/TaskSet separately from the Test class. Of course you would then need some way to pass the HttpSession into the Test class somehow (https://docs.locust.io/en/stable/testing-requests-based%20SDK%27s.html), which might not be easy. |
@cyberw Can you pleas let me know in which file the test collection strategy is defined for pytest? Do we have option to override/modify that? |
Sorry, you are asking the wrong person, I am in no way an expert on pytest :) |
Any one have idea, please let me know. |
@cyberw has anyone used the pytest api tests as locust tasks. We want to use the same pytest tests for performance testing. |
No, not that I know of. |
Some folks report or at least draft usage there on SO, but there's also a comment to one of the answers saying "This code may be outdated due to changes in locust. Locust made same breaking changes when it was upgraded to 1.0 release" https://stackoverflow.com/questions/55914502/how-can-i-perform-load-testing-with-locust-using-pytest |
Description of issue / feature request
Automatic interpretation of pytest tests as locust tasks
I already have a comprehensive pytest setup that tests a service API. I would like to be able to annotate these tests with locust tasks or similar so I can share this code base for both functional and performance testing
Is this fundamentally possible due to locust's event system?
Expected behavior
A simple set of instructions exist that allow me to configure locust to use my existing pytest tests
Actual behavior
I have to duplicate code between my functional and locust tests.
The text was updated successfully, but these errors were encountered: