Skip to content

Commit 934f908

Browse files
author
Alan
committed
Unit Testing
1 parent 4c2fa1d commit 934f908

8 files changed

Lines changed: 63 additions & 2 deletions

File tree

360 Bytes
Binary file not shown.
572 Bytes
Binary file not shown.

Chapter03/arithmetic.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# 在这个脚本中,我们将创建4个函数:add_numbers, sub_numbers, mul_numbers, div_numbers
2+
3+
def add_numbers(x, y):
4+
return x + y
5+
6+
def sub_numbers(x, y):
7+
return x - y
8+
9+
def mul_numbers(x, y):
10+
return x * y
11+
12+
def div_numbers(x, y):
13+
return (x / y)

Chapter03/if_example.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def check_if():
2+
a = int(input("Enter a number \n"))
3+
if(a == 100):
4+
print("a is equal to 100")
5+
else:
6+
print("a is not equal to 100")
7+
return a

Chapter03/test_addition.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import arithmetic
2+
import unittest
3+
4+
# Testing add_numbers function from arithmetic.
5+
class Test_addition(unittest.TestCase):
6+
# Testing Integers
7+
def test_add_numbers_int(self):
8+
sum = arithmetic.add_numbers(50, 50)
9+
self.assertEqual(sum, 100)
10+
# Testing Floats
11+
def test_add_numbers_float(self):
12+
sum = arithmetic.add_numbers(50.55, 78)
13+
self.assertEqual(sum, 128.55)
14+
#Testing Strings
15+
def test_add_numbers_strings(self):
16+
sum = arithmetic.add_numbers('hello','python')
17+
self.assertEqual(sum, 'hellopython')
18+
19+
if __name__ == '__main__':
20+
unittest.main()

Chapter03/test_if.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import if_example
2+
import unittest
3+
4+
class Test_if(unittest.TestCase):
5+
def test_if(self):
6+
result = if_example.check_if()
7+
self.assertEqual(result, 100)
8+
9+
if __name__ == '__main__':
10+
unittest.main()

Chapter03/trace_example.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Student:
2+
def __init__(self, std):
3+
self.count = std
4+
5+
def go(self):
6+
for i in range(self.count):
7+
print(i)
8+
return
9+
10+
if __name__ == "__main__":
11+
Student(5).go()

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
[第二章 Python脚本调试和性能测试](https://alanhou.org/debugging-profiling-python-scripts/):讲解如何使用调试工具来对Python程序进行调试。你还将学习如何进行错误处理,并深入性能测试(profiling)和耗时测试(timing)的概念。
1313

14-
[第三章 单元测试-单元测试框架的介绍[(https://alanhou.org/unit-testing-introduction-unit-testing-framework/):本章有关 Python 中的单元测试。我们将对测试程序创建单元测试。
14+
[第三章 单元测试-单元测试框架的介绍](https://alanhou.org/unit-testing-introduction-unit-testing-framework/):本章有关 Python 中的单元测试。我们将对测试程序创建单元测试。
1515

16-
第四章 自动化常规运维活动:讲解如何自动化运维管理员的常规运维活动。你将学习到接收输入、处理密码、外部命令的执行、读取配置文件、为脚本添加警告信息、实现 CPU 限制、网页浏览器启动、os模块的使用以及进行备份。
16+
[第四章 自动化常规运维活动](https://alanhou.org/automating-regular-administrative-activities/):讲解如何自动化运维管理员的常规运维活动。你将学习到接收输入、处理密码、外部命令的执行、读取配置文件、为脚本添加警告信息、实现 CPU 限制、网页浏览器启动、os模块的使用以及进行备份。
1717

1818
第五章 文件、目录和数据处理:本章你将学习使用os模块来处理各种活动。读者会学习到数据以及应用到数据上的一些方法,如复制、剪切、合并和比较。还将学习tarfile模块及如何及用这一模块。
1919

0 commit comments

Comments
 (0)