Created
December 5, 2019 01:41
-
-
Save FerdinaKusumah/8cfada8e1533f0995f9b7a6f61a623a4 to your computer and use it in GitHub Desktop.
Single Unit Test
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
import unittest | |
class ExampleTestUnit(unittest.TestCase): | |
def test_sum(self): | |
self.assertEqual(sum([1, 2, 3]), 6, "Hasilnya adalah 6") | |
def test_count_array(self): | |
array = [1, 2, 3, 4] | |
self.assertEqual(len(array), 4, "Jumlah arraynya ada 5") | |
def test_error(self): | |
""" | |
Ini adalah contoh unit tes untuk data yang salah, | |
jika kode ini dijalankan maka akan menghasilkan error | |
karena string tidak bisa di sum atau di jumlahkan :) | |
error ini akan di tangkap oleh python dan masuk kedalam eksepsi | |
bertipe TypeError | |
""" | |
fruits = "banana" | |
with self.assertRaises(TypeError): | |
sum(fruits) | |
if __name__ == "__main__": | |
suite = unittest.TestLoader().loadTestsFromTestCase(ExampleTestUnit) | |
unittest.TextTestRunner(verbosity=2).run(suite) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment