Skip to content

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

Notifications You must be signed in to change notification settings

kivanc57/pytesting

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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-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:

    git clone https://github.com/kivanc57/pytesting
    cd pytesting
  2. Set up the virtual environment:

    python3 -m venv venv
    source venv/bin/activate   # On Windows, use `venv\Scripts\activate`
  3. Install dependencies:

    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:

pytest

Run tests with verbose output:

pytest -v

Run specific tests (e.g., slow tests):

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_lengths.

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:
# conftest.py
@pytest.fixture
def test_rectangle():
    return shapes.Rectangle(length=10, width=20)

💡 Example Test Cases

  • Testing add function
def test_add():
    result = functions.add(number_one=1, number_two=4)
    assert result == 5
  • Testing classroom initialization
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 file for details.

─── ⋆⋅☆⋅⋆ ─────────────────────────────────────────────────────

📬 Contact

For any inquiries or contributions, please feel free to reach out.

About

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

Topics

Resources

Stars

Watchers

Forks

Languages