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

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
31 changes: 31 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import pytest
from pytest_mock import MockerFixture
from torchvision import datasets
from torch.utils.data import DataLoader
import torch.nn as nn
from src import main

def test_data_loading_and_preprocessing(mocker: MockerFixture):
"""Test the data loading and preprocessing steps."""
mock_mnist = mocker.patch.object(datasets, 'MNIST')
mock_dataloader = mocker.patch.object(DataLoader, '__init__')

main.load_and_preprocess_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_model_definition():
"""Test the model definition."""
model = main.Net()

assert isinstance(model, main.Net)
assert isinstance(model.fc1, nn.Linear)
assert model.fc1.in_features == 784
assert model.fc1.out_features == 128
assert isinstance(model.fc2, nn.Linear)
assert model.fc2.in_features == 128
assert model.fc2.out_features == 64
assert isinstance(model.fc3, nn.Linear)
assert model.fc3.in_features == 64
assert model.fc3.out_features == 10