-
Notifications
You must be signed in to change notification settings - Fork 41
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 unit tests for the functions in utils.py #63
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ install: | |
- pip install -r test-requirements.txt | ||
script: | ||
- flake8 . | ||
- pytest ./tests/test_utils.py | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import os | ||
from tempfile import TemporaryDirectory | ||
import numpy as np | ||
from PIL import Image | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sort the imports in this way:
|
||
from autowebcompat import utils | ||
|
||
|
||
def test_get_bugs(): | ||
bugs = utils.get_bugs() | ||
assert(isinstance(bugs, list)) | ||
|
||
|
||
def test_mkdir(): | ||
d = TemporaryDirectory() | ||
direc_path = d.name + "/test" | ||
utils.mkdir(direc_path) | ||
assert(os.path.isdir(direc_path)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Second part from #63 (comment) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @marco-c Instead of writing a test for it , would it not make more sense to change the mkdir function to check if a directory with the same name exists and not make it if it does? since the function the doesnt return any error message it is pretty hard to assert on anything . If some error message were returned i could assert on the same . There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't need to add an assertion, the assertion is implied in the fact that |
||
utils.mkdir(direc_path) | ||
|
||
|
||
def test_load_image(tmpdir): | ||
d = TemporaryDirectory() | ||
img = Image.new("RGB", (30, 30)) | ||
img.save(d.name + "/Image.jpg") | ||
img = utils.load_image("Image.jpg", d.name) | ||
assert(isinstance(img, np.ndarray)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can assert the dimensions of the numpy array too. |
||
|
||
|
||
def test_read_labels(): | ||
labels = utils.read_labels(file_name='labels.csv') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Create a fake labels.csv file in a temporary directory, so you can assert more things about the result. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, don't do it, we'll do it as part of the other test that writes and reads labels. |
||
assert(isinstance(labels, dict)) | ||
|
||
|
||
def test_write_labels(): | ||
label = {1: 1, 2: 2} | ||
d = TemporaryDirectory() | ||
file_path = d.name + "/test.csv" | ||
utils.write_labels(label, file_name=file_path) | ||
assert(os.path.exists(file_path)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remember to file an issue about adding a test that writes labels and then reads them back. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is going to execute
test_utils.py
only, please make it run any test that is in thetests
directory, so we don't have to update this line the next time we add a test.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't forget about this, @Shashi456