wenwen
wenwen

探索網路世界和進入區塊鏈的過程或許很平凡,但我就是想分享給大家,若能幫助到有需要幫助的人,那就更好了。 有幫到你的話,記得按下方拍手圖,不需花你任何金錢,免費註冊Liker ID,就可對我化讚為賞:https://button.like.co/willie5068

022第11章 TESTING YOUR CODE_續4_setUp 方法

今天農曆大年初一,早起晨讀真好

💛20230122晨讀感言:

💔我在寫 測試 class 程式時, 結合了之前 Class 的 `__init()__` 概念,事實證明, 就算你沒有在 Class 中寫下 def `__init()__` 方法, 你也可以利用 ==直接指定== 給予Attribute 一個值,或給予 Attribute 一個 你所建立的 instance ,最後,你還是可以 利用 sefl.Attribute 在下面 各種 test 方法中 使用。 (如下圖範例)

test_survey.py 的修改

💔完成上面的 測試程式 後,我在 本章 後面 接觸到 setUp() 這個方法,沒想到 它就相當於 Class 裡的 `__init()__` 方法,而它和 `__init()__` 方法 最大的差別,在於 `__init()__` 方法 裡的 Attributes 可經由 parameter 傳送或直接指定 來給予值。而 setUp() 方法 則是沒有 parameter 可傳送,它就單純的 直接指定 值 或 instance 給予 Attributes 。

💔呈上,在 setUp() 裡的 Attributes 如果有需要想讓 同一個 class中 的其它方法使用的話,那麼 Attributes 前面要加上 self. 才行。如果只是單純在 setUp() 裡 使用的話,那就不用加 self.(如下圖)

setUp 方法裡的 Attributes前面有加 self. 的區別

💛晨讀摘要:

💚220 The setUp() Method:In test_survey.py we created a new instance of AnonymousSurvey in each test method, and we created new responses in each method. The unittest.TestCase class has a setUp() method that allows you to create these objects once and then use them in each of your test methods. When you include a setUp() method in a TestCase class, Python runs the setUp() method before running each method starting with test_. Any objects created in the setUp() method are then available in each test method you write.

from:《python crash course》

CC BY-NC-ND 2.0 版权声明

喜欢我的文章吗?
别忘了给点支持与赞赏,让我知道创作的路上有你陪伴。

加载中…
加载中…

发布评论