[TIP] Starting pytest from a microservice framework

arun kali raja arunsep886 at gmail.com
Wed Jan 3 23:39:43 PST 2018

Thanks a lot Bruno,

One more requirement i have is to execute a set of test cases repeatedly in
a loop for a particular amount of time.. say 24 hours..

As per my understanding, in pytest when I do a loop, the different test
item objects are created according to the number of the loop at the startup
phase itself.. so the tests will be named test1[1/n],test[2/n]...test[n/n].

Now if i want to run for a particular duration how do i achieve that??

one way is to have user mark each testcase with the amount of time it will
take to complete and then decide how many iterations each of them should be
run.. Is there any other way i can achieve it?

Also i see that the order of execution of testcases is also not
maintained.. say i have test_1 run 3 times in a loop.. i see test_1[3/3]
run first and then test_1[1/3] is executed..

Arun Kaliraja.B

On 04-Jan-2018 00:24, "Bruno Oliveira" <nicoddemus at gmail.com> wrote:

Hi Arun,

You can call pytest.main() directly instead of the command line, but using
the command line in a sub-process is also reasonable IMHO (you might want
to send a full log of the run back to the client for example).

In order to stop test mid-execution you will need to implement your own
pytest_runtestloop hook in a conftest.py file that will verify some
condition between each run:


To keep things simple, upon each request for a new test session, create a
temporary directory and put your conftest.py file in there along with the


When receiving a request to stop the current run, you write a special named
file (".stop" for example), which your hook checks periodically: when it is
found it stops the current execution (probably by raising

Hope this helps!


On Wed, Jan 3, 2018 at 11:06 AM arun kali raja <arunsep886 at gmail.com> wrote:

> Hi,
> I have a requirement where in i want to start pytest from a microservice
> framework.
> So the user will be sending a restAPI request and based on that i should
> be able to do the service will start a pytest session.
> i have couple of questions on how to achieve this.
> 1. All pytest invocations i see is from cmd line.. can i invoke the pytest
> run from within another piece of code?
> 2. If yes then can i run it as a seperate thread/process all together from
> the service?
> 3. If yes to that also.. suppose i want to handle rest requests like stop
> testrun in between how do i achieve it.. i dont want to kill the thread..
> once the ongoing test is completed then i should stop the execution
> gracefully. python threading library offers event objects to achieve such
> things.. i am not sure if i will be able to do such things in a
> conftest.py..
> Regards
> Arun Kaliraja.B
> --
> அன்புடன்,
> பா. அருண் காளி ராசா
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20180104/46bc6ad4/attachment.htm>

More information about the testing-in-python mailing list