python娴嬭瘯鏂█
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python娴嬭瘯鏂█相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/user' title='user'>user
鎺掗敊 python san 寮傚父 mit 妯″潡 class 杩愯銆€銆€杩欏嚑澶╃湅浜嗕竴涓媝ython鐨勬祴璇曪紝鍩轰簬鍑芥暟鏂规硶鍜屽熀浜庣被鐨勬祴璇曪紝涓昏浣跨敤鐨勬ā鍧楁槸unittest妯″潡锛屼负鐗瑰畾鐨勬柟娉曞拰绫伙紝寤虹珛娴嬭瘯妯″潡锛屾祴璇曞嚱鏁板姛鑳芥槸鍚︽弧瓒抽鏈熴€備笅闈㈡槸妯′豢涔﹂噷闈㈢殑涓€涓緥瀛愩€?/p>
銆€銆€City.py妯″潡
def city(city_name,country_name): full_city_name = city_name +鈥?/span>,鈥?/span>+country_name return full_city_name
銆€銆€涓鸿繖涓ā鍧楀垱寤轰竴涓祴璇曟ā鍧楋紝涓昏鏄负浜嗘祴璇曡繖涓猚ity鏂规硶鏄惁鑳藉婊¤冻棰勬湡銆?/p>
銆€銆€assert妯″潡
import unittest from City import city class Testcity(unittest.TestCase): def test_city(self): city_name = 鈥?/span>Santiago鈥?/span> country_name = 鈥?/span>Chile鈥?/span> full_city_name = city(city_name,country_name) print(full_city_name) self.assertEqual(full_city_name,鈥?/span>Santiago,Chile鈥?/span>) # self.assertEqual(4+5,9) if __name__ == 鈥?/span>__main__鈥?/span>: unittest.main()
銆€銆€涓昏灏辨槸瀵煎叆unittest妯″潡锛岀劧鍚庡畾涔変竴涓祴璇曠被锛岀户鎵縰nittest.TestCase锛屽湪娴嬭瘯绫讳腑瀹氫箟娴嬭瘯鏂规硶锛岄拡瀵逛簬琚祴璇曟柟娉曪紝缂栧啓娴嬭瘯鐢ㄤ緥锛岀劧鍚巃ssert鏂█锛屾煡鐪嬭緭鍏ワ紝濡傛灉鍦ㄨ繍琛岃繃绋嬩腑鍑虹幇鎶ラ敊锛屽垯璇佹槑妯″潡鍔熻兘鏈夐棶棰橈紝闇€瑕佷慨鏀瑰姛鑳姐€傚鏋滄祴璇曟病鏈夐棶棰橈紝鍒欒瘉鏄庢祴璇曡兘澶熼€氳繃锛屽姛鑳芥ā鍧楁病鏈夐棶棰樸€?/p>
銆€銆€
銆€銆€杩欏氨鏄祴璇曟垚鍔熺殑缁撴灉锛屽鏋滄祴璇曢儴鎴愭灉锛屽涓嬪浘鎵€绀恒€?/p>
銆€銆€
銆€銆€浼氭姏鍑轰竴涓紓甯革紝鍦ㄦ柇瑷€鐨勫湴鏂广€傜劧鍚庡彲浠ユ牴鎹繖涓紓甯革紝鏉ヨ繘琛屾帓閿欍€?/p>
銆€銆€
銆€銆€涓€鑸紪鍐欑殑浠g爜锛岄兘闇€瑕佺粡杩囨祴璇曚箣鍚庯紝鎵嶈兘姝e父浣跨敤銆傞伩鍏嶅鏉傜殑浜哄伐娴嬭瘯锛宎ssert鏂█鑳藉鍋氬埌鑷姩鍖栵紝涓€鏃︽湁鎶ラ敊锛屽垯鐩存帴鑳藉垽鏂姛鑳芥槸鍚︽甯搞€?/p>
銆€銆€涓嬮潰鏄竴涓绫荤殑娴嬭瘯鏂█锛屽啓鍦ㄥ悓涓€涓ā鍧椾腑锛屽彧鏄负浜嗚鏄庢祴璇曟柟娉曘€?/p>
class Employee(): def __init__(self,first_name,last_name,salary): self.first_name = first_name self.last_name = last_name self.salary = int(salary) def give_raise(self,raise_default = 5000): self.salary += raise_default class TestClassEmployee(unittest.TestCase): def setUp(self): self.username = Employee(鈥?/span>John鈥?/span>,鈥?/span>Smith鈥?/span>,鈥?/span>60000鈥?/span>) def test_give_default_raise(self): self.username.give_raise() new_raise = 65000 self.assertEqual(new_raise,self.username.salary) def test_give_custom_raise(self): self.username.give_raise(7000) new_raise = 67000 self.assertEqual(new_raise, self.username.salary)
if __name__ == 鈥?/span>__main__鈥?/span>: unittest.main()
銆€銆€瀹氫箟浜嗕竴涓憳宸ョ被锛岄渶瑕佺煡閬撳憳宸ョ殑鍚嶅瓧鍜岃柂姘达紝鍚屾椂瀹氫箟浜嗕竴涓定宸ヨ祫鏂规硶锛岄粯璁ゅ€兼槸5000锛屽湪瀹氫箟娴嬭瘯绫荤殑鏃跺€欙紝棣栧厛瀹氫箟浜嗕釜娴嬭瘯瀵硅薄锛岄€氳繃setUp鏂规硶锛岀敓浜ф祴璇曞璞★紝鍚庣画鐨勬祴璇曟柟娉曚腑锛屽彲浠ラ€氳繃璇ユ祴璇曞璞℃潵鍋氭祴璇曘€傚畾涔変簡涓や釜娴嬭瘯鏂规硶锛岄粯璁ゅ€煎拰鑷畾涔夊€笺€傛祴璇曠粨鏋滃涓嬶細
銆€銆€
以上是关于python娴嬭瘯鏂█的主要内容,如果未能解决你的问题,请参考以下文章