利用 ChatGPT 來幫你寫 Test Case 單元測試!

做為一個程式開發者,最重要的就是寫好測試案例,讓我們的程式能夠在每次發版前,至少都確認關鍵邏輯不會因為程式的修改而出錯。而 ChatGPT 是一個後起之秀,我們也可以利用 ChatGPT 來完善你的測試案例,怎麼用呢?

chatgpt icon

◎◎ 網站 小檔案◎◎
■ 網站名稱: ChatGPT
■ 網站介面:英文
■ 是否需註冊:
■ 是否免費:
■ 網站網址:https://openai.com/blog/chatgpt

舉例來說,我需要寫一個案例,測試客戶輸入的日期是否為聖誔節,這時我們可以這樣問他。

please give me a smoking test case for test the input date is X'mas with user story

而 ChatGPT 也很聰明的回答。

User Story: As a user, I want to receive a notification on Christmas day to remind me not to smoke.

Test Case:

Preconditions: The user has the app installed and has allowed notifications.
Input: Date: December 25th, Time: 12:00 pm
Actions: The system should check if the input date matches the Christmas day and if the time is set to 12:00 pm.
Expected Results: The system should send a notification to the user reminding them not to smoke on Christmas day at 12:00 pm. The notification should be clear and easy to understand.
Postconditions: The notification should be received by the user and they should be reminded not to smoke on Christmas day.
Note: This is just an example test case, and in real-world scenarios, there would be many more test cases to cover different scenarios and edge cases.

大家可以看一下圖,可以知道 ChatGPT 其實超聰明。

chatgpt 1

那我們再來一個複雜的案例,當我們買賣股票時,通常買進的交割金額的公式為下

買進股票的交割金額  = 買進股票的價格 * 股數 +  (買進股票的價格 * 股數) *0.01425*券商手續費折數

賣出的交割金額為

賣出股票的交割金額  = 賣出股票的價格 * 股數 - (賣出股票的價格 * 股數) *0.01425*券商手續費折數 - (賣出股票的價格 * 股數) * 交易稅

所以,我們可以請 ChatGPT 給我們一個測試案例,雖然英文不是很好,但他看得懂就好。

Please give me a smoke test case for stock trading. The transaction tax is 0.001, which is only charged when selling, and not charged when buying. The brokerage fee is the price multiplied by the number of shares traded times 0.01425 times 0.6, and the brokerage fee is the lowest It is 1 dollars, will be charged both on buy and sell, all amounts are rounded down to integers, please give me the positive income of buying and selling stocks

同樣的,他會馬上給你一個測試案例,雖然測試案例算出來的結果可能錯,但基本上已經提供給你一個寫測試案例的方向了。

Sure, here's an example smoke test case for stock trading:

Scenario: Buying and Selling Stocks

Test Case Description:

1.User logs in to the trading platform
2.User searches for a stock and finds a stock with a current price of $10 per share
3.User decides to buy 100 shares of the stock, and places a buy order
4.The platform charges a brokerage fee of $6 (100 * $10 * 0.01425 * 0.6), which is rounded down to the nearest integer, so the fee charged is $6
5.User's account is debited with $1000 (100 shares * $10 per share) plus $6 brokerage fee, for a total of $1006
6.The user holds on to the stock for a few days, and the stock price rises to $12 per share
7.The user decides to sell all 100 shares, and places a sell order
8.The platform charges a brokerage fee of $7 (100 * $12 * 0.01425 * 0.6), which is rounded down to the nearest integer, so the fee charged is $7
9.The platform also charges a transaction tax of $1.2 (100 * $12 * 0.001), which is rounded down to the nearest integer, so the tax charged is $1
10.User's account is credited with $1193 (100 shares * $12 per share) minus $7 brokerage fee and $1 transaction tax, for a total of $1185
11.The user's profit from buying and selling the stock is $1185 - $1006 = $179

Expected Result:

The expected result of this smoke test case is that the user earns a positive income of $179 from buying and selling the stocks. 
The platform should charge the correct brokerage fee and transaction tax as per the given rules,
and all amounts should be rounded down to the nearest integer as mentioned.

同樣,大家可以看一下圖,可以知道 ChatGPT 其實超聰明。

chatgpt 2
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments