Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests using mocker to main.py #32

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import pytest
from unittest.mock import Mock, call
from PIL import Image
import torch
from torchvision import datasets, transforms
from torch.utils.data import DataLoader
import numpy as np
from src import main

def test_data_loading(mocker):
"""
Test the data loading step in main.py.
This test checks that the datasets.MNIST and DataLoader functions are called with the correct arguments.
"""
mock_mnist = mocker.patch('torchvision.datasets.MNIST', return_value=[])
mock_dataloader = mocker.patch('torch.utils.data.DataLoader', return_value=[])

main.load_data()

mock_mnist.assert_called_once_with('.', download=True, train=True, transform=main.transform)
mock_dataloader.assert_called_once_with(mock_mnist.return_value, batch_size=64, shuffle=True)

def test_data_preprocessing(mocker):
"""
Test the data preprocessing step in main.py.
This test checks that the transforms.Compose function is called with the correct arguments.
"""
mock_compose = mocker.patch('torchvision.transforms.Compose', return_value=[])

main.preprocess_data()

mock_compose.assert_called_once_with([
transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,))
])
Loading