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>

銆€銆€鎶€鏈垎浜浘鐗? src=

銆€銆€杩欏氨鏄祴璇曟垚鍔熺殑缁撴灉锛屽鏋滄祴璇曢儴鎴愭灉锛屽涓嬪浘鎵€绀恒€?/p>

銆€銆€鎶€鏈垎浜浘鐗? src=鎶€鏈垎浜浘鐗? src=

銆€銆€浼氭姏鍑轰竴涓紓甯革紝鍦ㄦ柇瑷€鐨勫湴鏂广€傜劧鍚庡彲浠ユ牴鎹繖涓紓甯革紝鏉ヨ繘琛屾帓閿欍€?/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鏂规硶锛岀敓浜ф祴璇曞璞★紝鍚庣画鐨勬祴璇曟柟娉曚腑锛屽彲浠ラ€氳繃璇ユ祴璇曞璞℃潵鍋氭祴璇曘€傚畾涔変簡涓や釜娴嬭瘯鏂规硶锛岄粯璁ゅ€煎拰鑷畾涔夊€笺€傛祴璇曠粨鏋滃涓嬶細

銆€銆€鎶€鏈垎浜浘鐗? src=

 

以上是关于python娴嬭瘯鏂█的主要内容,如果未能解决你的问题,请参考以下文章

鎬ц兘娴嬭瘯涔嬫柊涓€浠f湇鍔″櫒鎬ц兘娴嬭瘯宸ュ叿Gatling

鑺墖娴嬭瘯

閲嶆瀯-鏋勭瓚娴嬭瘯浣撶郴

銆愮洿鎾€戞暣杞﹂┚涔樻€ц兘娴嬭瘯璇勪环瑙e喅鏂规

[CSP-S妯℃嫙娴嬭瘯96]棰樿В