File tree Expand file tree Collapse file tree 3 files changed +73
-0
lines changed Expand file tree Collapse file tree 3 files changed +73
-0
lines changed Original file line number Diff line number Diff line change 1+ import unittest
2+
3+ def koneksi_ke_db ():
4+ print ("[terhubung ke db]" )
5+
6+ def putus_koneksi_db (db ):
7+ print ("[tidak terhubung ke db {}]" .format (db ))
8+
9+ class User :
10+ username = ""
11+ aktif = False
12+
13+ def __init__ (self , db , username ): # using db sample
14+ self .username = username
15+
16+ def set_aktif (self ):
17+ self .aktif = True
18+
19+ class TestUser (unittest .TestCase ):
20+ # Test Case 1
21+ def test_user_default_not_active (self ):
22+ db = koneksi_ke_db ()
23+ dicoding = User (db , "dicoding" )
24+ self .assertFalse (dicoding .aktif ) # tidak aktif secara default
25+ putus_koneksi_db (db )
26+
27+ # Test Case 2
28+ def test_user_is_active (self ):
29+ db = koneksi_ke_db ()
30+ dicoding = User (db , "dicoding" )
31+ dicoding .set_aktif () # aktifkan user baru
32+ self .assertTrue (dicoding .aktif )
33+ putus_koneksi_db (db )
34+
35+ if __name__ == "__main__" :
36+ # Test Runner
37+ unittest .main ()
Original file line number Diff line number Diff line change 1+ import argparse
2+
3+ # python3 ./Python/Testing/main.py -n Billy -a 20
4+
5+ parser = argparse .ArgumentParser ()
6+
7+ parser .add_argument ('-n' , '--name' , required = True , help = "Enter your name" )
8+ parser .add_argument ('-a' , '--age' , required = True , help = "Enter your age" )
9+
10+ args = parser .parse_args ()
11+
12+ print ("Hey " + args .name + " what's good?" )
13+ print ("You are " + args .age + " years old." )
Original file line number Diff line number Diff line change 1+ import unittest
2+
3+ class TestStringMethods (unittest .TestCase ):
4+ # Ini adalah test case pertama (1)
5+ def test_strip (self ):
6+ self .assertEqual ('www.dicoding.com' .strip ('c.mow' ), 'dicoding' )
7+
8+ # Test case kedua (2)
9+ def test_isalnum (self ):
10+ self .assertTrue ('c0d1ng' .isalnum ())
11+ self .assertFalse ('c0d!ng' .isalnum ())
12+
13+ # Test case ketiga (3)
14+ def test_index (self ):
15+ s = 'dicoding'
16+ self .assertEqual (s .index ('coding' ), 2 )
17+ # cek s.index gagal ketika tidak ditemukan
18+ with self .assertRaises (ValueError ):
19+ s .index ('decode' )
20+
21+ if __name__ == '__main__' :
22+ # Test Runner
23+ unittest .main ()
You can’t perform that action at this time.
0 commit comments