Pytest command cheat sheet

Option

Why is it needed?

--collect-only

Used for collecting information about tests without actually executing them. When you run this command pytest collects and displays a list of all tests it finds in the specified file or directory, but does not run them. This can be useful for checking which tests will be executed, especially in large projects where it is important to know what tests exist and how they are organized. The command output is typically presented as a tree showing the structure of the tests, allowing you to quickly see which tests are available to run.

-k "name"

Used for filter tests by name. With this parameter, you can specify part of the test name or label, and pytest will only run tests that match the specified pattern.

-m "mark"

Used for filtering tests by tags (markers). With this parameter you can specify a label, and pytest will only run tests that have the specified label.

-x

Used for stopping tests after the first failure. This means that if one of the tests fails, pytest will stop running the remaining tests and report the error that occurred.

--maxfail=N

Used for stop running tests after N failures. This means that if N tests fail, pytest will stop running the remaining tests and display a report of any errors that occurred.

--lf

Used for rerun only those tests that failed in the last run. This can be useful when you want to quickly check and fix only those tests that previously failed, without having to run all the tests again.

--ff

Used for launch tests, while those tests that failed in the previous one are executed first launch. This allows you to focus on fixing bugs, since failing tests will be checked first before other tests are run.

-v

Used for increasing the level of detail in output while running tests. When using this command pytest will display more detailed information about each test, including its name and status (success or failure). This makes it easier to keep track of which tests passed and which ones failed, which can be useful for debugging and analyzing test results.

-q

Used for reducing the level of detail in output while running tests. When using this command pytest will output only the minimum necessary information, allowing you to focus on the main test results, such as the number of tests passed and failed, without additional details about each test.

-l

Used for display local variables in output when an error occurs in tests. When a test fails, this command allows you to see the values ​​of local variables at the time of failure, which can greatly simplify the debugging process and help you quickly identify the cause of the error.

--tb=<style>

Used for settings for the traceback format when errors occur in tests. Parameter <style> can take on different meanings, such as auto, long, short, lineand others that determine how error information will be displayed.

auto (default): Uses the long trace format for the first and last records, and the short trace format for the rest.

long : Provides a comprehensive and informative trace.

short : Displays more compact information.

line : Displays the trace as a single line.

--durations=N

Used for runtime display tests where N – This number of slowest teststhat you need to see in the report. This is especially useful for optimizing tests and improving overall test execution time.

--version

Used for display current version installed framework pytestwhich can be useful for checking compatibility with other libraries or diagnosing problems.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *