An open API service indexing awesome lists of open source software.

https://github.com/kivanc57/pytesting

This repository demonstrates pytest testing techniques, including fixtures, mocking, and parametrization, with tests for operations and custom classes.
https://github.com/kivanc57/pytesting

class mock-testing parametrized-tests pytest python python3 testing

Last synced: about 2 months ago
JSON representation

This repository demonstrates pytest testing techniques, including fixtures, mocking, and parametrization, with tests for operations and custom classes.

Awesome Lists containing this project

README

        

# Pytesting ๐Ÿงช

This repository provides a comprehensive testing suite for validating geometric shapes, mathematical functions, and a fictional Hogwarts-themed classroom model. With a set of pre-defined tests, mocks, and parameterizations, this project demonstrates extensive testing using `Pytest`.

โ”€โ”€โ”€ โ‹†โ‹…โ˜†โ‹…โ‹† โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

## Table of Contents
- [๐Ÿ—๏ธ Project Structure](#project-structure)
- [๐Ÿš€ Installation](#installation)
- [๐Ÿ› ๏ธ Usage and Execution](#usage-and-execution)
- [๐Ÿ”ฌ Testing Components](#testing-components)
- [๐Ÿ”Ž Sample Code and Fixtures](#sample-code-and-fixtures)
- [๐Ÿงฉ Notes](#notes)
- [โš™๏ธ Requirements](#requirements)
- [๐Ÿค Contributing](#contributing)
- [๐Ÿ“œ License](#license)
- [๐Ÿ“ฌ Contact](#contact)

โ”€โ”€โ”€ โ‹†โ‹…โ˜†โ‹…โ‹† โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

# ๐Ÿ—๏ธ Project Structure
```markdown
๐Ÿ“ project-root
โ”œโ”€โ”€ ๐Ÿ“ source
โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ __init__.py
โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ functions.py
โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ school.py
โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ service.py
โ”‚ โ””โ”€โ”€ ๐Ÿ“„ shapes.py
โ”‚
โ”œโ”€โ”€ ๐Ÿ“ tests
โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ __init__.py
โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ conftest.py
โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ test_Circle.py
โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ test_functions.py
โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ test_Rectangle.py
โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ test_school.py
โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ test_service.py
โ”‚ โ””โ”€โ”€ ๐Ÿ“„ test_Square.py
โ”‚
โ”œโ”€โ”€ ๐Ÿ“„ .gitignore
โ”œโ”€โ”€ ๐Ÿ“„ .gitattributes
โ”œโ”€โ”€ ๐Ÿ“„ requirements.txt
โ””โ”€โ”€ ๐Ÿ“„ test_commands.txt
```

โ”€โ”€โ”€ โ‹†โ‹…โ˜†โ‹…โ‹† โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

## ๐Ÿš€ Installation

1. **Clone the repository:**
```bash
git clone https://github.com/kivanc57/pytesting
cd pytesting
```

2. **Set up the virtual environment:**
``` python
python3 -m venv venv
source venv/bin/activate # On Windows, use `venv\Scripts\activate`

3. Install dependencies:
``` python
pip install -r requirements.txt
```

โ”€โ”€โ”€ โ‹†โ‹…โ˜†โ‹…โ‹† โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

## ๐Ÿ› ๏ธ Usage and Execution
### Running Tests
The testing suite is built with Pytest and can be executed with various options:

Run all tests:
``` bash
pytest
```

Run tests with verbose output:
``` bash
pytest -v
```

Run specific tests (e.g., slow tests):
``` bash
pytest -m slow
```

### Mock Tests
Mocks are utilized in this project to simulate external calls (e.g., API requests) and avoid dependencies on real databases or services.

### Parameterized Tests
This project also employs parameterized testing for the shapes module, allowing multiple test cases with different inputs.

โ”€โ”€โ”€ โ‹†โ‹…โ˜†โ‹…โ‹† โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

## ๐Ÿ”ฌ Testing Components
### **Shapes Module Tests**
Tests cover various shapes, including:

- ๐Ÿ”ท **Rectangle** ๐Ÿ”ท

Validates `area()` and `perimeter()` methods, ensuring correct calculations.

- ๐Ÿ”ด **Circle** ๐Ÿ”ด

Checks `area()` and `perimeter()` for circular dimensions.

- ๐ŸŸฉ **Square** ๐ŸŸฉ

Uses parameterized testing to validate multiple `side_length`s.

### Classroom Module Tests
Simulates a Hogwarts-style classroom:

- ๐Ÿซ **Classroom Initialization** ๐Ÿซ

Ensures teacher and students are correctly assigned.

- ๐Ÿ‘จ๐Ÿปโ€๐ŸŽ“ **Student Addition and Removal** ๐Ÿ‘จ๐Ÿปโ€๐ŸŽ“

Tests adding/removing students and handles maximum capacity constraints.

- ๐Ÿ‘ฉ๐Ÿปโ€๐Ÿซ **Teacher Assignment** ๐Ÿ‘ฉ๐Ÿปโ€๐Ÿซ

Validates the teacher's assignment and reassignment.

### Mocked Tests
Uses unittest.mock to:

* **Mock Database Calls**: Validates responses from database functions without actual database dependencies.

* **Mock API Calls**: Tests API calls and error handling without hitting real endpoints.

โ”€โ”€โ”€ โ‹†โ‹…โ˜†โ‹…โ‹† โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

## ๐Ÿ”Ž Sample Code and Fixtures
- **Fixtures for Reusable Test Objects**: Fixtures in conftest.py help set up reusable test objects for shapes and the classroom environment. One of them is below:

``` python
# conftest.py
@pytest.fixture
def test_rectangle():
return shapes.Rectangle(length=10, width=20)
```

๐Ÿ’ก **Example Test Cases**
* Testing add function
``` python
def test_add():
result = functions.add(number_one=1, number_two=4)
assert result == 5
```

* Testing classroom initialization
``` python
def test_classroom_initialization(hogwarts_classroom):
assert hogwarts_classroom.teacher.name == "Minerva McGonagall"
assert len(hogwarts_classroom.students) == 5
```

โ”€โ”€โ”€ โ‹†โ‹…โ˜†โ‹…โ‹† โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

### ๐Ÿงฉ Notes
* Skip Tests:
Some tests are marked as `@pytest.mark.skip` if a feature is broken or in development.

* Expected Failures:
`@pytest.mark.xfail` marks tests that are expected to fail under certain conditions.

โ”€โ”€โ”€ โ‹†โ‹…โ˜†โ‹…โ‹† โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

## โš™๏ธ Requirements
* Python 3.7+
* Pytest
* Mock (for testing API/database dependencies)
* Requirements specified in `requirements.txt`

โ”€โ”€โ”€ โ‹†โ‹…โ˜†โ‹…โ‹† โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

## ๐Ÿค Contributing
Contributions are welcome! If you would like to contribute to this project, please fork the repository and submit a pull request with your changes.

โ”€โ”€โ”€ โ‹†โ‹…โ˜†โ‹…โ‹† โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

## ๐Ÿ“œ License
This project is licensed under the GNU GENERAL PUBLIC LICENSE - see the [LICENSE](https://github.com/kivanc57/pytesting/blob/main/LICENSE) file for details.

โ”€โ”€โ”€ โ‹†โ‹…โ˜†โ‹…โ‹† โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€

## ๐Ÿ“ฌ Contact

For any inquiries or contributions, please feel free to reach out.
- **GitHub Profile**: [kivanc57](https://github.com/kivanc57)
- **Email**: [[email protected]](mailto:[email protected])