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 #83

Closed
wants to merge 6 commits 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
32 changes: 32 additions & 0 deletions src/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from unittest import mock

import pytest
from torch.utils.data import DataLoader
from torchvision import datasets

from main import Net, trainloader, transform


def test_data_loading_and_preprocessing():
"""
Test the data loading and preprocessing steps.
"""
with mock.patch.object(datasets, "MNIST") as mock_mnist:
with mock.patch.object(DataLoader, "__init__") as mock_dataloader:
mock_mnist.return_value = None
mock_dataloader.return_value = None
pytest.assert_not_none(trainloader)
mock_mnist.assert_called_once_with(
".", download=True, train=True, transform=transform
)
mock_dataloader.assert_called_once_with(None, batch_size=64, shuffle=True)


def test_model_definition():
"""
Test the model definition.
"""
with mock.patch.object(Net, "__init__", return_value=None) as mock_net:
model = Net()
pytest.assert_not_none(model)
mock_net.assert_called()
Loading