There is a way to define a method that executes when a task set starts; ie, on_start(). It would be useful to have a similar method, on_quit() when the task set is terminated.
I want my task set to share a common identifier. I create this in on_start(). A side effect of the tests is to create an artifact with this identifier in the name. When the tests terminate, I want the artifact to be deleted.
I've tried using the events.on_quitting event for this. However, as far as I can tell, this is called in the context of the Locust object, not in the context of the task itself.
+1
I don't get why on_quit() was not there from the start. It seems like such an obviously necessary functionality.
Well, that would be a great feature, something like a cleanup for automated tests.
In my case there are a lot of test users registering and creating some content, it would be nice to have a way to automatically remove all of this when the test are done (though, in real life I would rather do it myself, so I can inspect the data in case if something goes wrong during the tests)
+1, the equivalent of tearDown is needed to remove data created during the load test.
I'm using this simple workaround at the moment. Since on_quit is a bound method, it still has the task set's context when it's executed.
class MyTaskSet(TaskSet):
def on_start(self):
locust.events.quitting += self.on_quit
def on_quit(self):
...
+1
+1 this is a must have if engs need to continue working with this framework.
Also would opt in for this one. Solution suggested by @ze-phyr-us works good if you have plain structure (With only one task_set), but doesn't really work with nested tasksets as they won't execute their teardown up until whole locust process quits.
So that's definitely a must have here.
How is on_quit() different from on_stop()?
this is an old request that has already been implemented: https://docs.locust.io/en/stable/writing-a-locustfile.html#setups-teardowns-on-start-and-on-stop
Most helpful comment
+1, the equivalent of
tearDownis needed to remove data created during the load test.I'm using this simple workaround at the moment. Since
on_quitis a bound method, it still has the task set's context when it's executed.