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.
- Host: GitHub
- URL: https://github.com/kivanc57/pytesting
- Owner: kivanc57
- License: gpl-2.0
- Created: 2024-11-09T16:45:46.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2025-01-13T21:34:25.000Z (4 months ago)
- Last Synced: 2025-02-10T08:49:12.504Z (4 months ago)
- Topics: class, mock-testing, parametrized-tests, pytest, python, python3, testing
- Language: Python
- Homepage: https://www.freecodecamp.org/news/testing-in-python-with-pytest/
- Size: 39.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: READMe.md
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
```โโโ โโ โโ โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
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.โโโ โโ โโ โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
For any inquiries or contributions, please feel free to reach out.
- **GitHub Profile**: [kivanc57](https://github.com/kivanc57)
- **Email**: [[email protected]](mailto:[email protected])