python|March 03, 2021|1 min read

Python - Some useful Pytest Commands

TL;DR

Use pytest command line options like file path filtering, -k for keyword matching, -s for print output, and --log-cli-level for enabling log output during test runs.

Python - Some useful Pytest Commands

Introduction

In this post, we will explore some useful command line options for running python unit tests.

Running Selective Test File

Assumming you have lot of tests written, and is in seperate files.

To run only tests within a particular file, you just pass the path to that file only.

pytest <path_to_pytest_file>

Running Selective Test case across

Many times, you may want tp run only one test case across your pool of test cases. You would want to run it by name.

pytest -k <test_name>

Note: It will run all those test cases where this test_name is occurring. It works kind of `string contains’.

So if your test names are:

test_check_1
test_check_2
test_check_3
test_check_10
test_check_12

And, if you run

pytest -k test_check1

It will run following test cases:

test_check_1
test_check_10
test_check_12

You may have some print statements in between and want to know the output.

pytest -s

You can use it with -k option as well.

pytest -k check_1 -s

Enable Logging in Pytest

Often times, the Python logging does not come in pytest. To enable this,

pytest --log-cli-level=INFO 

pytest --log-cli-level=INFO -k check_1 -s

Related Posts

ReactJS - How to pass method to component and how to pass arguments to method

ReactJS - How to pass method to component and how to pass arguments to method

Introduction In the ReactJS project, you are having aa parent component and a…

Drupal 8&#58; How to Export and Import View

Drupal 8&#58; How to Export and Import View

You have created some views, and want to port it to your production environment…

VS-Code - How to Debug and pass Command Line Arguments via Launch Config

VS-Code - How to Debug and pass Command Line Arguments via Launch Config

Introduction In this post, I will take example for Python project. And how we…

Curl - Your friend for Rest APIs/Calls - Basic Commands

Curl - Your friend for Rest APIs/Calls - Basic Commands

Curl is a wonderful tool for initiate REST APIs or calls. Or, you can literally…

How to Patch and Build Python 3.7.9 for FIPS enabled Openssl

How to Patch and Build Python 3.7.9 for FIPS enabled Openssl

Introduction In this post, we will see Python 3.7.9 patch for FIPS enabled…

Python - How to read files in multiple ways

Python - How to read files in multiple ways

Read file all in one shot Above code read whole file at once, but if the files…

Latest Posts

REST API Design: Pagination, Versioning, and Best Practices

REST API Design: Pagination, Versioning, and Best Practices

Every time two systems need to talk, someone has to design the contract between…

Efficient Data Modelling: A Practical Guide for Production Systems

Efficient Data Modelling: A Practical Guide for Production Systems

Most engineers learn data modelling backwards. They draw an ER diagram…

Deep Dive on Caching: From Browser to Database

Deep Dive on Caching: From Browser to Database

“There are only two hard things in Computer Science: cache invalidation and…

System Design Patterns for Real-Time Updates at High Traffic

System Design Patterns for Real-Time Updates at High Traffic

The previous articles in this series covered scaling reads and scaling writes…

System Design Patterns for Scaling Writes

System Design Patterns for Scaling Writes

In the companion article on scaling reads, we covered caching, replicas, and…

System Design Patterns for Managing Long-Running Tasks

System Design Patterns for Managing Long-Running Tasks

Introduction Some operations simply can’t finish in the time a user is willing…