Skip to content

Approaching software testing and data science / maching learning using pytest.

Notifications You must be signed in to change notification settings

hugodscarvalho/data-science-testing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation


Testing & Data Science

Software Engineering Practices
Explore the docs »

Pytest documentation · Unittest documentation · Robot documentation

Table of Contents
  1. Pytest
  2. Getting Started
  3. Usage
  4. Roadmap
  5. Contributing
  6. License
  7. Contact
  8. Acknowledgements

Pytest

Introduction

Pytest is a testing framework based on python. It is mainly used to write API test cases. This tutorial helps you understand:

  • Installation of pytest
  • Various concepts and features of pytest
  • Sample programs

By the end of this tutorial, you should be able to start writing test cases using pytest.

Prerequisites

The prerequisites to begin with this tutorial are:

  • Familiarity with any programming language.
  • Knowledge of basic programming concepts.
  • Python installed.

Getting Started

Pytest is a python based testing framework, which is used to write and execute test codes. In the present days of REST services, pytest is mainly used for API testing even though we can use pytest to write simple to complex tests, i.e., we can write codes to test API, database, UI, etc.

Advantages of Pytest

The advantages of Pytest are as follows:

  • Pytest can run multiple tests in parallel, which reduces the execution time of the test suite.
  • Pytest has its own way to detect the test file and test functions automatically, if not mentioned explicitly.
  • Pytest allows us to skip a subset of the tests during execution.
  • Pytest allows us to run a subset of the entire test suite.
  • Pytest is free and open source.
  • Because of its simple syntax, pytest is very easy to start with.

In this tutorial, we will explain the pytest fundamentals with sample programs.

Environment Setup

To install the latest version of pytest, execute the following command −

pip install pytest

Confirm the installation using the following command to display the help section of pytest.

pytest --help

Identifying Test files and Test Functions

Running pytest without mentioning a filename will run all files of format test_*.py or *_test.py in the current directory and subdirectories. Pytest automatically identifies those files as test files. We can make pytest run other filenames by explicitly mentioning them.

Pytest requires the test function names to start with test. Function names which are not of format test* are not considered as test functions by pytest. We cannot explicitly make pytest consider any function not starting with test as a test function.

We will understand the execution of tests in our subsequent chapters.

Contributing

Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Starting with basic test

Now, we will start with our first pytest program. We will first create a directory and thereby, create our test files in the directory.

Let us follow the steps shown below:

  • Create a new directory named pytest and navigate into the directory in your command line.

  • Create a file named test_square.py and add the below code to that file.

import math

def test_sqrt():
   num = 25
   assert math.sqrt(num) == 5

def testsquare():
   num = 7
   assert 7*7 == 40

def tesequality():
   assert 10 == 11

Contact

Your Name - @your_twitter - email@example.com

Project Link: https://github.com/your_username/repo_name

Acknowledgements

Releases

No releases published

Packages

No packages published

Languages