[go: up one dir, main page]

0% found this document useful (0 votes)
29 views11 pages

Pytest Command Guide and Usage

The document provides an overview of the PyTest framework, including commands for running tests, installation instructions, and guidelines for naming test files and functions. It explains the use of assertions in Python, the creation of test files, and how to run tests across multiple files and directories. Additionally, it covers the use of markers to label and selectively run tests, as well as various pytest features like skipping tests and parameterization.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views11 pages

Pytest Command Guide and Usage

The document provides an overview of the PyTest framework, including commands for running tests, installation instructions, and guidelines for naming test files and functions. It explains the use of assertions in Python, the creation of test files, and how to run tests across multiple files and directories. Additionally, it covers the use of markers to label and selectively run tests, as well as various pytest features like skipping tests and parameterization.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd

PyTest

Commands:
pytest
pytest [Link]
[Link]
pytest -rA
pytest -rA -k login_fun
pytest [Link] -rA -k login_fun
pytest -rA –junitxml=”Report_file.xml” -- gives all testcases report as file
pytest –fixtures
pytest --markers
pytest –help

Note :
1. all methods and filenames have to start with test_ or end with _test.
2. We can run multiple files in one dir by using pytest or [Link].
3. Pytest will automatically run the methods and subdirectories which are starts
with test .i.e 1 point
4. pytest -rA gives all detailed suceed or why failed
-r means pytest flags
run pytest –help gives all about pytest flags
A - means all testcases info.
F - only failed tests

5.

Ex :
single files multi methods
filename : test_1.py

def test_p1(): def test_p2():


assert 10 == 20 assert 10 == 10
$ pytest or [Link] $ pytest or [Link]
output: 1 failed assertion error output: 1 passed and 1 failed
Pytest:

The pytest framework makes it easy to write small, readable tests, and can scale to support complex
functional testing for applications and libraries.

What is assert in Python?


The assert statement is used to test if a condition is true. If the condition is false, Python raises
an AssertionError.

Syntax : assert <condition>


assert <condition>, "Error message if assertion fails"

1 : Installation

pytest requires: Python 3.8+ or PyPy3.


Goto command line in PyCharm
1. Run the following command in your command line:
pip install -U pytest

2. Check that you installed the correct version:


$ pytest --version
pytest 8.4.1

2 : File creation

- Create a new file called test_sample.py

- all filenames have to start with test_ or end with _test.

- all methods have to start with test_ only to test automatically.

- directories/packages can be any name

3 : Run

$: pytest or [Link] – goes to the files which are started or ended with test_ or _test and call the
methods started with test_in in our current dir.

$: pytest [Link] – tests only par file funs in cur directory

$: pytest -rA – gives short test summery


$: pytest -rF -gives short test summery about failed testcases.

$: pytest -v -gives each test with detailed info (PASSED, FAILED, SKIPPED, etc.)

$: pytest -k <word> – tests only funs which having word in funs name, remaining funs will be
deselected it won’t run.
Create your first test file

Created file test_file1.py


# content of test_file1.py
def func(x):
return x +2

def test_answer():
assert func(3) == 5

$:pytest
$:

- Created Package Pytest

- created a file test_file1.py (starts with test_)

- wrote a normal method and a test method, test methods defaultly called and if any assertion error
occurs it will raise assertion error.
Multiple test methods in single file

test_file1.py:

def func(x):
return x + 2

def normal_fun():
assert "hi" in "hehe"

def test_1():
assert func(3) == 5

def test2_test():
# normal_fun()
assert "hi" in "hello hi", "hi not found"

Pytest$ pytest test_file1.py

- it will only calls funs starts or ends with test_ and it does not call normal_fun and [Link] you want
to call normal methods fun calls should be in test methods.
If you uncomment nornal_fun then gives assertionerror,cuz hi is not in hehe
Multiple Files in Single Dir

- Create multiple files start with test_ in single dir or packge, write methods and run using
pytest -rA

- collected : 3 test methods are found in cur dir.

- 3 files in current directory are tested


- 2 test methods are passed and one method test2_test failed and takes 0.06 sec to run.

Multiple directories

- create files as shown in below and cd to the PytestDemo dir, cuz we are testing test
methods in Pytest_pack which contains normal_dir,Pytest1,Pytest2 packages and
run.
- It will show the info of testcases.
- the directory can be package or dir anything works.
- we can use path also in pytest command it will go that par dir/file and runs test
methods.
Run only files contains particular name (-k word)

- Created file test_file2.py


def test_file2_fun(): #test fun
assert 5 == 5
def test_login_fun(): #test fun
assert 2==2
def test_login(): #test fun
assert 1 ==1
def test_ji_helogin_hey(): #test fun
assert 1==0
#normal method
def logins_test():
assert 2==2

$ pytest test_file2.py -k login


- testing funs which contains login in the funs name.

- 4 test files are there found test_login_fun,test_login, test_ji_helogin_hey, test_file2_fun


- only 3 are running 1 test file which doesnot have login( test_file2_fun) is not running which is
deselected.
- logins_test is a normal method we didn’t call anywhere,thats why it is not tested.

Note : If you forgot to give – or k or neither it won’t work gives Error, have to give -k
Pytest markers

- Markers in pytest are used to label tests so you can selectively run, group them.
$: pytest --markers
- gives all available markers

@[Link](reason=None)
-skip the given test function with an optional reason. Example: skip(reason="no way of currently
testing this") skips the test.
- markers all available in pytest package, we have import inorder to use.
- skip marker will skip the test from running, and gives as skipped files
- reason is option when we give reason it will show as the status of that fun
- by using this we are skipping running some tests.
- test_1.py consists of 2 skip markers and 1 test file.
- skip without reason gives as uncoditional skip, i.e. not skipping based on any condition

@[Link](condition, ..., *, reason=...)


-skip the given test function if any of the conditions evaluate to True.
-Example:
skipif([Link] == 'win32') # skips the test if we are on the win32 platform.
Ex:
import pytest
def test_met():
print("test method")

@[Link](1==0,reason="1 is equal to 1")


def test_nor():
print("skip marker")
assert 2==2
@[Link](reason="skiped 2nd marker")
def test_nor2():
print("skip marker")
assert 2==1

test_not status is skipped


skipif will skip the test if the condition is true, it runs if it is false.

@[Link](condition, ..., *, reason=..., run=True, raises=None, strict=xfail_strict) -


mark the test function as an expected failure if any of the conditions evaluate to True. Optionally
specify a reason for better reporting and run=False if you don't even want to execute the test
function. If only specific exception(s) are expected, you can list them in raises, and if the test fails
in other ways, it will be reported as a true failure.

@[Link](argnames, argvalues) - call a test function multiple times passing in


different arguments in turn. argvalues generally needs to be a list of values if argnames specifies
only one name or a list of tuples of values if argnames specifies multiple names. Example:
@parametrize('arg1', [1,2]) would lead to two calls of the decorated test function, one with arg1=1
and another with arg1=2.
assert 2==2

You might also like