Skip to main content

Command Palette

Search for a command to run...

Unit Tests in Python

Week-5

Updated
2 min read
Unit Tests in Python

ဒီအပတ်ရဲ့သင်ခန်းစာအများစုကတော့ အရင်ကသိပြီးသားအကြောင်းအရာတွေပဲဖြစ်ပါတယ်။ အဲ့ဒီသိပြီးသားအကြောင်းအရာတွေကိုမှ ဘယ်လိုဟာတွေသတိထားရမယ်၊ codeကိုပိုကောင်းအောင် ဘယ်လိုရေးသားလို့ရတယ်ဆိုတာတွေကို ပြောပြသွားပါတယ်။ အဲ့ဒါအပြင် ကိုယ်ရေးထားတဲ့ code ကို ကိုယ်တိုင်ပြန်စစ်ရလွယ်ကူအောင် သုံးနိုင်တဲ့ function တွေကိုလည်း ပြောပြထားပါတယ်။

ပထမဆုံးအနေနဲ့ ကိုယ်ပိုင် function တွေကို def နဲ့တည်ဆောက်ပြီး program runတဲ့အခါ function name ကို ပြန်ခေါ်ဖို့ မမေ့အောင် သတိထားရပါမယ်။ ကိုယ်တည်ဆောက်ထားတဲ့ main function ကို တခြား file တစ်ခုမှာ ခေါ်သုံးတာ၊ library အနေနဲ့ယူသုံးတာမျိုးလုပ်ဖြစ်ခဲ့ရင် အဲ့ main function လိုက်ကြီး auto run မသွားအောင် main function ကို ပြန်ခေါ်မသုံးခင်

if __name__  = "__main__": 
    main()

ဆိုပြီး အခြေအနေစစ်ပါတယ်။

Corner case ဆိုတာက မှားနိုင်တဲ့ case တွေထဲကမှ မထင်မှတ်ထားတဲ့ case တွေပဲဖြစ်ပါတယ်။ အဲ့တာကြောင့် bug ပေါ်လာနိုင်သမျှ corner case အားလုံးကို စစ်ဆေးထားဖို့လိုပါတယ်။ code ကို ပြန်ပြီး check လုပ်တဲ့အခါမှာဆိုရင် ဖြစ်နိုင်ခြေတွေအကုန်လုံးကို စစ်ဖိုဆိုတာ လုံးဝမဖြစ်နိုင်ပါဘူး။ အဲ့ဒီအစား ကိုယ်စမ်းနေတဲ့ function ပေါ်မူတည်ပြီး ဘယ်ကဏ္ဍမှာကတော့ ဘယ်လိုဖြစ်နိုင်တယ်ဆိုတဲ့ corner cases အပိုင်းလေးတွေကိုပဲစစ်မယ်ဆိုရင်အချိန်ကုန်သက်သာပြီး code ကိုလည်း တိုစေပါတယ်။

အဲ့လိုစစ်တဲ့နေရာမှာ "assert" ဆိုတဲ့ keyword ကို အသုံးပြုနိုင်ပါတယ်။ "assert"ကို သုံးတဲ့အခါ assert လုပ်လိုက်တဲ့ function ရဲ့ output ကအဖြေမှန် နဲ့ ညီရင် True ဖြစ်ပြီး Test ကို passဖြစ်သွားမှာပါ ။ condition အမှား၊ outputအမှားကြီးကို assert လုပ်မိရင်တော့ AssertionError တက်မှာပဲဖြစ်ပါတယ်။TypeError, SyntaxError တို့တုန်းကလိုပဲ try - except ကိုသုံးပြီး AssertionError ကိုဖမ်းနိုင်ပြီး ဘာဆက်လုပ်ရမလဲဆိုတာကိုလည်း ‌ဆက်ရေးသားနိုင်ပါတယ်။

တကယ်လို့ square တင်မဲ့ integer ကို integer မပြောင်းခဲ့ပဲ string အတိုင်းပဲထားခဲ့မယ်ဆိုရင်တော့ TypeError တက်မှာပဲဖြစ်ပါတယ်။နောက်ထပ်သိထားရမယ့်အကြောင်းအရာကတော့ pytest library ထဲက function raises() ပါ သူကတော့ pytest စစ်တဲ့အချိန်မှာတက်တဲ့ error ကို except လုပ်လိုက်တဲ့သဘောမျိုးနဲ့တူပြီး with နဲ့တွဲသုံးပါတယ်။

with pytest.raises(exceptions):
    function(value)

Function တွေကို assert နဲ့စစ်မယ်ဆိုရင် function ထဲမှာ return ပြန်ခဲ့မှစစ်လို့ရပါမယ်။ return မထားခဲ့ရင် side effect အနေနဲ့ function ထဲက ဘာမှထွက်မလာဘူးဆိုပြီးယူဆမှာဆိုတော့ error တက်ပါမယ်။

နောက်တစ်ခုကတော့ package လေးတွေပဲဖြစ်ပါတယ် package ကတော့ python module တစ်ခုထဲ ဒါမှမဟုတ် တစ်ခုထက်ပိုတဲ့ module တွေစုထားတဲ့ folder ကို package လို့ခေါ်ပါတယ်။ ဥပမာ ကိုယ်ရေးမယ့် fille name ကို "__init__" ဒီလို နှစ်ခုစီနဲ့ name ပေးလိုက်မယ်ဆိုရင်ဒီ file ကို package လို့သတ်မှတ်သွားမှာပဲဖြစ်ပါတယ်။

Pytest ဆိုတာက ကျွန်တော်တို့ရဲ့ python program တစ်ခုကို မှန်/မမှန်စမ်းသပ်ပေးနိုင်တဲ့ third-party library တစ်ခုဖြစ်ပြီး အဲ့ဒါကို pip နဲ့ install လို့ရပါတယ်။ pytest ကို terminal မှာ pytest ဆိုပြီး နောက်မှာ ကိုယ် test လုပ်ချင်တဲ့ python file name ကို ထည့်သုံးပြီးစမ်းလို့ရပါတယ်။ ဒါပေမယ့် pytest က test နဲ့ စတဲ့ function တွေကိုပဲ စစ်သွားမှာပါ။

import twttr

def test_vowel():
    assert twttr.shorten("Hello")=="Hll"
def test_capital_num():
    assert twttr.shorten("LUKE05")=="LK05"
def test_small_punctuation():
    assert twttr.shorten("allie?")=="ll?"

Pytest က ကိုယ့် program ရဲ့ function တစ်ခုကိုပဲ မှန်မမှန်စစ်ပေးတာဖြစ်ပြီး program တစ်ခုလုံးကို test လုပ်ပေးတာမဟုတ်ပါဘူး။ အဲ့တာကြောင့်မို့ pytest မှာ run လို့ မှန်တိုင်း ကိုယ်ရဲ့ program တစ်ခုလုံးက မှန်နေမယ်လို့ မှတ်ယူလို့မရပါဘူး။

pytest နဲ့ code တေ test လုပ်ချင်ရင်တော့ အဆင့်ဆင့်ရှိပါတယ်။

1. ကိုယ်စစ်မယ့် program ကို side effect မဖြစ်အောင် function value တခုခု return ပြန်ခဲ့ရန်

2. test program ရဲ့အောက်မှာ function names တွေပြန်ခေါ်စရာမလို

3. ခါတိုင်းလို python filename.py လို့ run ရမယ့်အစား pytest filename.py ဆိုပြီး run

4. အကုန် စိမ်းရင် ကိုယ့် program မှန်၊ နီရင် တစ်ခုခုမှားနေ

ကိုယ့် function တစ်ခုခုကို test ပြန်လုပ်တဲ့ code က function ပေါ်မူတည်ပြီး ရိုးရှင်းနေရင်ပိုကောင်းပါတယ်။

ကျွန်တော်တို့ test လုပ်ချင်တာက file တစ်ခုတည်းမဟုတ်ဘူး folder လိုက်ကြီးရှိတယ်ထားပါတော့၊ ဒါဆို ဘယ်လိုစစ်မလဲ။ အခြား folder က ကိုယ်စစ်ချင်တဲ့ program file တွေကို ဘယ်လိုလှမ်းစစ်မလဲ။

Step 1. Create a file name with “__init__.py” in the folder you want to test.

Step 2. run "pytest yourfoldername"

step 1 ကိုလုပ်လိုက်ချင်းအားဖြင့် python က package တစ်နည်းအားဖြင့် module အများကြီးလို့ မှတ်ယူသွားပြီး pytest က folder ထဲမှာရှိတဲ့ module တွေ၊ တစ်နည်းအားဖြင့် ".py" file တွေအများကြီးကို တစ်ချိန်ထဲမှာစစ်ဆေးပေးသွားပါတယ်။

အဆုံးထိဖတ်ရှုပေးလို့ ကျေးဇူးအထူးတင်ရှိပါတယ်။


Weekend Warriors

  • 6 members : 1 girl and 5 boys

    • 14 to 18 years old

    • learning programming for a year

More from this blog

Infrastructure ကိုင်ပြီး အိပ်ရေးမပျက် ချင် လျှင် ဒါမျိုး Alarms လုပ် 🔥🔥🔥

High Level ရေးထားတာပါ ဒါပေမဲ့ လွယ်ပါတယ် ​ကိုယ့်မှာ AWS Infra တွေရှိတယ်ဆို တွေ့သမျှ metric တွေကို alarms တွေလုပ်ပြီး notification ယူမနေဘဲ တကယ် effective ဖြစ်တဲ့ metric တွေကိုမှ CloudWatch ရဲ့ alarm feature တွေနဲ့ ပေါင်းပြီး ပို့စေချင်ပါတယ်။ ​ဥပမာ prod...

Jan 17, 20263 min read172
Infrastructure ကိုင်ပြီး အိပ်ရေးမပျက် ချင် လျှင်  ဒါမျိုး Alarms လုပ် 🔥🔥🔥

How to connect On Premises Network and Cloud (AWS)? (Part-2)

ကိုယ့်ရဲ့ ‌data center (on-prem) network နဲ့ AWS ချိတ်ဆက်ဖို့ လိုလာပြီဆိုရင် ဘယ်လို ချိတ်ဆက်ကြမလဲ? အပိုင်း (၂) မှာ တော့ Direct connect အကြောင်းကို ဆွေးနွေး သွားမှာ ဖြစ်ပါတယ်။ အပိုင်း (၁) Site-to-site VPN အကြောင်းကို လေ့လာချင်ရင်တော့ အောက်ပါ link မှာ ...

Dec 20, 20253 min read234
How to connect On Premises Network and Cloud (AWS)? (Part-2)

How to connect On Premises Network and Cloud (AWS)? (Part-1)

ကိုယ့်ရဲ့ ‌data center (on-prem) network နဲ့ AWS ချိတ်ဆက်ဖို့ လိုလာပြီဆိုရင် ချိတ်ဆက်နိုင်တဲ့ နည်း (၂) နည်း ရှိပါတယ်။ 1. Site-to-Site VPN (Virtual Private Network) 2. Direct connect Site-to-Site VPN - On-prem network နဲ့ AWS resources တွေ ချိတ်ဆက်တဲ့...

Dec 12, 20252 min read272
How to connect On Premises Network and Cloud (AWS)? (Part-1)

Accessibility for Designer

လွန်ခဲ့တဲ့အပတ်က ရုံးက Designer တွေနဲ့ တော်ကီပွားရင်း Accessibility နဲ့ပတ်သတ်တာတွေ သူတို့ကို ရှင်းပြဖြစ်တယ်။ ကိုယ်တိုင်ကလည်း အရင်ကတည်းက ဒီ topic ကိုစိတ်ဝင်စားလို့ လေ့လာနေတာဆိုတော့ အခွင့်အရေးရရင် ရသလို sharing လုပ်ဖြစ်တယ်။ အဓိကက Accessibility နဲ့ပတ်သတ်...

Nov 21, 20253 min read75
Accessibility for Designer

VPC Endpoint

အားလုံးဘဲ မင်္ဂလာပါ။ ဒီနေ့ sharing လုပ်ပေးချင်တာကတော့ VPC Endpoint အကြောင်းဘဲဖြစ်ပါတယ်။ VPC Endpoint ဆိုတာ VPC နဲ့ AWS services တွေ ကို public Internet ကို အသုံးမပြုဘဲနဲ့ Privately connect လုပ်ပေးတာ ဖြစ်ပါတယ်။ Internet Gateway, NAT Gateway နဲ့ Public...

Nov 18, 20252 min read101
VPC Endpoint
M

Myanmar Technical Blog

108 posts

Cloud, Linux, DevOps, Docker, Security အစရှိတဲ့ နည်းပညာများ အကြောင်းကို မြန်မာလို ပြန်လည်မျှဝေပေးမယ့် Blog ပဲဖြစ်ပါတယ်ခဗျာ...