Mikko Ohtamaa
Mikko Ohtamaa

Reputation: 83656

Printing test execution times and pinning down slow tests with py.test

I am running unit tests on a CI server using py.test. Tests use external resources fetched over network. Sometimes test runner takes too long, causing test runner to be aborted. I cannot repeat the issues locally.

Is there a way to make py.test print out execution times of (slow) test, so pinning down problematic tests become easier?

Upvotes: 267

Views: 75990

Answers (3)

baumannalexj
baumannalexj

Reputation: 875

Can I ask why the test needs to access external data? What type of test is it? When you say "external" do you mean outside of your immediate service but inside your org's domain, or do you mean outside of your org's domain?

I'm curious if we can challenge if it's needed at all to fetch external resources -- After spending time developing automated e2e, component, and integration testing, it's best to follow the test pyramid and focus on unit testing first which mock all immediate dependencies. Once you get to a boundary it's best practice to look into contract testing as much as possible.

Ask

  1. Why can't I mock?
  2. If you have to, then where can I mock? (where's the nearest boundary).

Upvotes: 0

Bruno Oliveira
Bruno Oliveira

Reputation: 15315

I'm not sure this will solve your problem, but you can pass --durations=N to print the slowest N tests after the test suite finishes.

Use --durations=0 to print all.

Upvotes: 394

Phuong
Phuong

Reputation: 1383

You can pass the number with --durations

pytest --durations=0 — Show all times for tests and setup and teardown

pytest --durations=1 — Just show me the slowest

pytest --durations=50 — Slowest 50, with times, … etc

Take refer in: https://medium.com/@brianokken/pytest-durations-0-show-all-times-for-tests-and-setup-and-teardown-848dccac85db

Or: https://docs.pytest.org/en/latest/usage.html#profiling-test-execution-duration

Upvotes: 96

Related Questions