If you want to benchmark your code while testing with pytest, try pytest-benchmark. The code above shows what pytest-benchmark does.
Link to pytest-benchmark.
Link to the source code.