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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ starlette==0.27.0
sympy==1.12
tomli==2.0.1
tomlkit==0.12.1
torch==2.1.0+cpu
torch==1.8.1
torchaudio==2.1.0+cpu
torchvision==0.16.0+cpu
typing_extensions==4.8.0
Expand Down
30 changes: 30 additions & 0 deletions src/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import unittest
from unittest.mock import patch
import torch
from torchvision import datasets, transforms
from torch.utils.data import DataLoader
from main import Net, transform

class TestMain(unittest.TestCase):
def setUp(self):
self.model = Net()
self.transform = transform

def test_Net(self):
# Test the forward pass
input_tensor = torch.randn(1, 1, 28, 28)
output = self.model(input_tensor)
self.assertEqual(output.size(), (1, 10))

@patch('torchvision.datasets.MNIST')
@patch('torch.utils.data.DataLoader')
def test_data_loading(self, mock_dataloader, mock_dataset):
# Mock the MNIST dataset
mock_dataset.return_value = datasets.MNIST('.', download=True, train=True, transform=self.transform)
# Mock the DataLoader
mock_dataloader.return_value = DataLoader(mock_dataset, batch_size=64, shuffle=True)
# Assert that the DataLoader is called with the correct arguments
mock_dataloader.assert_called_with(mock_dataset, batch_size=64, shuffle=True)

if __name__ == '__main__':
unittest.main()
Loading