Last active
September 10, 2016 13:52
-
-
Save KentaYamada/82f56742d8b464b9d2dfb27fa89a0383 to your computer and use it in GitHub Desktop.
ソースコード、テストコード、テストデータを分離
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*-coding: utf-8 -*- | |
| def add(x, y): | |
| return x + y |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| expected | x | y | |
|---|---|---|---|
| 2 | 1 | 1 | |
| 3 | 2 | 1 | |
| 4 | 4 | 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [ | |
| {"expected": 2, "x": 1, "y": 1}, | |
| {"expected": 3, "x": 2, "y": 1}, | |
| {"expected": 4, "x": 4, "y": 0} | |
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*-coding: utf-8 -*- | |
| import unittest | |
| import csv | |
| from hoge import add | |
| def get_testdata(filename): | |
| if filename is None: | |
| raise IOError("filename cannot empty.") | |
| rows = [] | |
| with open(filename, mode='r') as f: | |
| reader = csv.reader(f) | |
| next(reader) # skip header coloumns | |
| rows = [row for row in reader] | |
| return rows | |
| def get_testdata_from_json(filename): | |
| if filename is None: | |
| raise IOError("filename cannot empty.") | |
| data = [] | |
| with open(filename, mode='r') as f: | |
| data = json.load(f) | |
| return data | |
| class TestHoge(unittest.TestCase): | |
| def test_add(self): | |
| test_data = get_testdata("hoge_testdata.csv") | |
| for data in test_data: | |
| result = add(int(data[1]), int(data[2])) | |
| self.assertEqual(int(data[0]), result) | |
| #jsonのが直感的でいいね! | |
| def test_add_from_json(self): | |
| test_data = get_testdata_from_json("hoge_testdata.json") | |
| for data in test_data: | |
| result = add(data['x'], data['y']) | |
| self.assertEqual(data['expected'], result) | |
| if __name__ == "__main__": | |
| unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment