or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
使用 Pytest 進行單元測試
Max Lai
I hear and I forget.
I see and I remeber.
I do and I understand.
今天的 demo code
https://github.com/cclai999/pytest-0706
為什麼需要寫測試呢?
執行環境準備(PyCharm Setup for Pytest)
PyCharm Setup for Pytest 執行環境準備
Install pytest
建立 PyCharm 專案後,在 Terminal 用 pip 安裝
Pytest 官方文件:https://docs.pytest.org/
測試 Pytest 是否能正確執行
demo project
I hear and I forget.
I see and I remeber.
I do and I understand.
unit test vs pytest
第一個 unit test
hello.py
unittest
test_hello_unittest.py
執行 unittest
pytest
test_hello_pytest.py
執行 pytest
"F" 表示測試沒有通過,如果出現 "." 則表示成功
執行 pytest
pytest fail message
"F" 表示測試沒有通過,如果出現 "." 則表示成功
pytest fail message
何謂單元測試(1/2)
Unit tests are typically automated tests written and run by software developers to ensure that a section of an application (known as the "unit") meets its design and behaves as intended. In procedural programming, a unit could be an entire module, but it is more commonly an individual function or procedure.
何謂單元測試(2/3)
何謂單元測試(3/3)
PyCharm Demo
練習 test_hello.py (10分鐘)
三種測試的出口驗證
名詞解釋-SUT
有些人會稱為 CUT (Class Under Test or Code Under Test)
1st SUT 驗證
2st SUT 驗證
3rd SUT 驗證
pytest-如何測試 exception 的狀況
production code (calculator.py)
pytest-如何測試 exception 的狀況
test code (test_calculator.py)
File names and function names
PyCharm Demo
練習 test_calculator.py (15分鐘)
單元測試的命名
pytest-Parametrizing tests
production code (triangle.py)
pytest-Parametrizing tests
test code (test_triangle.py)
PyCharm Demo
練習 test_triangel.py (15分鐘)
3A 原則
一個單元測試通常包含了三個行為
pytest-Fixtures
在單元測試之前先建立好變數或物件,可重覆利用
pytest-Fixtures
pytest-Fixtures: scope
controls how often a fixture gets set up and torn down.
pytest-Fixtures: autouse
get a fixture to run all of the time
pytest-Fixtures: yield
優秀單元測試的特性:
參考資料