0% found this document useful (0 votes)
8 views12 pages

CS Unit 4-1

This document provides an overview of using the Pytest framework for testing in Python, including installation instructions, file naming conventions, and how to run tests. It highlights the advantages of Pytest, such as its simple syntax and ability to run tests in parallel. Additionally, it covers the use of assertions in Pytest and includes a basic example program.

Uploaded by

Shreeya Rao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views12 pages

CS Unit 4-1

This document provides an overview of using the Pytest framework for testing in Python, including installation instructions, file naming conventions, and how to run tests. It highlights the advantages of Pytest, such as its simple syntax and ability to run tests in parallel. Additionally, it covers the use of assertions in Pytest and includes a basic example program.

Uploaded by

Shreeya Rao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

PYTHON FOR COMPUTATIONAL PROBLEM

SOLVING
Team python
Department of Computer Science & Engineering
PYTHON FOR COMPUTATIONAL
PROBLEM SOLVING

Unit-4:Testing

Team python
Department of Computer Science & Engineering
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
Agenda

➢ Introduction.
➢ Pytest installation .
➢ File identification and test methods.
➢ How to run ?
➢ Assertion in pytest.
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
Introduction
➢ Testing is a process by which one or more expected behaviors
and results from a piece of software are exercised and confirmed.
➢ Large set of tools and libraries are available in python library for automated testing.

Python testing framework are:


➢ Robot

➢ PyTest

➢ Unittest

➢ DocTest

➢ Nose2

➢ Testif

➢ We will discuss only pytest framework.


PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
Advantages of pytest

➢Very easy to start with because of its simple and easy syntax.
➢Can run tests in parallel.
➢Can run a specific test or a subset of tests
➢Automatically detect tests
➢Skip tests
➢Open source
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
Pytest installation

To install the pytest, execute the following command:


pip install pytest==2.9.1

To confirm the installation of pytest,use the following command:


py.test -h
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
File identification and test methods
➢ File name has to starts with test_ .py or ends with * _test.py .
➢ Test function name has to start withtest_one.py
“test”. - valid
Examples of pytest file names one_test.py - valid
testtwo.py -invalid
twotest.py -invalid

Examples of test method


def test_method(): - valid
def testmethod(): - valid
def method_test(): - invalid
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
How to run?

➢ To run only specific file test cases, use py.test <filename>


py.test test_example1.py

➢ If you need more information, use -v or –verbose:


py.test test_example1.py -v

➢ To run more than one but not all the files in the folder use py.test
<filename1><filename2><filename3> and so on.
py.test test_one.py test_example1.py

➢ To run all the pytest files in the directory :


py.test
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
Assertions in PyTest

Assertions are expression that return the status either True or False based on the
condition .

Syntax
assert <condition>,<error message>

Examples
assert "corona" == "corona2020" is an assertion failure.
assert 2==2 is a successful assertion
assert True is a successful assertion
assert False is an assertion failure.
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
Pytest first program
1.Create the new directory named “testing “or any other name and navigate into your
directory from the command line.

2.Create the file named “test_example1.py” and add the below code and save it.

import pytest
def func(x):
return x + 1

def test_answer():
assert func(2) == 3
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
References
1.docs.pytest.org.
THANK YOU
Team Python
Department of Computer Science and Engineering
*Chitra G M, Neeta Ann Jacob, Sangeetha R, Jeny Jijo,
Pushpa G ,Supreetha R ,Rachana B S.

Contact Email ID’s:


[email protected]
[email protected]

You might also like