AAARGH, WHY TESTING IS SO DIFFICULT?!!




Hello everyone, here I am again, Rafiano. I wanna tell all of you that I was screaming all day all night "WHY CREATING TEST IS SO EFFIN' DIFFICULT?!". Yes, literally. You could probably see my commit in AT-1 branch how desperately I am periodically change my test code for hours, during the night. 

It all changed this morning. Thanks to a very clear explanation about back-end testing, start from creating mock csv to how to create a proper test by allocateam's best lead engineer, Wicaksono (Well he don't want to admit that he is tho lol). I can't imagine how many more hours would I probably have to spend if there's no explanation by him.

So, it all started when I creating this one, haven't used mocker yet and I implement the logic (using selection and repetition) as a test to check whether it contains all the key or not:

class TestParser(unittest.TestCase):
def setUp(self):
self.app = mario.app.test_client()
self.app.testing = True
def test_parse_return_not_none(self):
# TODO use mocker
data = p.parse(None, None)
self.assertNotEqual(data, None)
def test_parse_return_correct_format(self):
# TODO use mocker
data = p.parse(None, None)
correct = False
if "res" in data:
if data["res"] is not None:
filled = True
for i in data["res"]:
if (i["branch"] is not None) and (i["data"] is not None):
all_data = True
for key in i["data"]:
# cek semua atribut
if i["data"][key] is None:
all_data = False
break
if not all_data:
filled = False
break
correct = filled and all_data
self.assertTrue(correct)

Long story short, I was keep refactoring the code like change the test to check whether the output is a valid json or not, is a dict or not, and so on and so on. Until this morning, Wicaksono tell me that it's not how you create a test. He told me that a test simply call a method, pass the proper arguments (using mocker for this case ofc), and simply assert with an expected output. He also told me how to create a mock csv for this case. Now, my testing is what a test should be (thanks again to my friend, Wicaksono). Here's my test now:

class TestParser(unittest.TestCase):
def setUp(self):
self.app = mario.app.test_client()
self.app.testing = True
def test_parse_return_valid_format(self):
file_names = list(validator.TABLE_COLUMN.keys())
files = []
mock_data = {
'branch': [1, 'Tebet', 'Jl. Tebet Raya', 'Rafiano Ruby',
'08131651104', [1], [1]],
'penyuluh': [1, 'Wicaksono Wisnu', '08111710107'],
'territory': [1, 'DKI Jakarta', 'Jakarta Timur',
'Duren Sawit', 'Malaka Jaya'],
'ketua_arisan': [1, 'Bthari Smart', 1, 'Jl. Bunga Rampai',
-6.198495, 106.837306]
}
for filename in file_names:
file = StringIO()
writer = csv.writer(file)
writer.writerow(validator.TABLE_COLUMN[filename])
writer.writerow(mock_data[filename])
files.append(file)
data = p.parse(files[0], files[1], files[2], [files[3]])
expected_output = {
"res":
[
{
"branch": "Tebet",
"address": "Jl. Tebet Raya",
"branch_manager":
{
"name": "Rafiano Ruby",
"phone_number": "08131651104"
},
"penyuluh":
[
{
"name": "Wicaksono Wisnu",
"phone_number": "08111710107"
}
],
"territory":
{
"province": "DKI Jakarta",
"city": "Jakarta Timur",
"subdistrict": "Duren Sawit",
"village": "Malaka Raya"
},
"ketua_arisan":
[
{
"name": "Bthari Smart",
"address": "Jl. Bunga Rampai",
"latitude": -6.198495,
"longitude": 106.837306
}
]
]
} }
self.assertEqual(data, expected_output)

Finally, now I can stop doing this task and can be more productive for my next task(s).

So that's all for me now, Ciao!

Image source:

0 komentar:

Post a Comment