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

Python - How to Maintain Quality Build Process Using Pylint and Unittest Coverage With Minimum Threshold Values

Python - How to Maintain Quality Build Process Using Pylint and Unittest Coverage With Minimum Threshold Values

Introduction It is very important to introduce few process so that your code and…

Python SMTP Email Code - How to Send HTML Email from Python Code with Authentication at SMTP Server

Python SMTP Email Code - How to Send HTML Email from Python Code with Authentication at SMTP Server

Introduction This post has the complete code to send email through smtp server…

Example Jenkin Groovy Pipeline Script for Building Python Projects with Git Events and Push to Artifactory

Example Jenkin Groovy Pipeline Script for Building Python Projects with Git Events and Push to Artifactory

Introduction In this post, we will see a sample Jenkin Pipeline Groovy script…

Python - How to Implement Timed-Function which gets Timeout After Specified Max Timeout Value

Python - How to Implement Timed-Function which gets Timeout After Specified Max Timeout Value

Introduction We often require to execute in timed manner, i.e. to specify a max…

How to Solve Circular Import Error in Python

How to Solve Circular Import Error in Python

Introduction To give some context, I have two python files. (Both in same folder…

Python Code - How To Read CSV with Headers into an Array of Dictionary

Python Code - How To Read CSV with Headers into an Array of Dictionary

Introduction Lets assume we have a csv something similar to following: Python…

Latest Posts

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

Server Security Best Practices — Complete Hardening Guide for Production Systems

Server Security Best Practices — Complete Hardening Guide for Production Systems

Every breach post-mortem tells the same story: an unpatched service, a…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF have been in the OWASP Top 10 for over a decade. They’re among the…

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

The OWASP Top 10 is the industry standard for web application security risks. If…