以不同的测试方法写十个测试用生例
时间: 2024-03-15 17:46:17 浏览: 68
好的,下面是十个测试用例,分别使用了不同的测试方法:
1. 边界值分析法:
```python
def test_divide():
assert divide(1, 1) == 1
assert divide(0, 1) == 0
assert divide(1, 0) == "Error: division by zero"
assert divide(2147483647, 1) == 2147483647
assert divide(-2147483648, 1) == -2147483648
assert divide(1, 2147483647) == 0
assert divide(1, -2147483648) == 0
assert divide(-2147483648, -1) == 2147483648
assert divide(2147483647, -1) == -2147483647
assert divide(2147483647, 2) == 1073741823
```
2. 等价类划分法:
```python
def test_login():
assert login("username", "password") == "Login successful"
assert login("", "password") == "Error: invalid username or password"
assert login("username", "") == "Error: invalid username or password"
assert login(" ", "password") == "Error: invalid username or password"
assert login("username", " ") == "Error: invalid username or password"
assert login(None, "password") == "Error: invalid username or password"
assert login("username", None) == "Error: invalid username or password"
```
3. 因果图法:
```python
def test_shopping_cart():
shopping_cart = ShoppingCart()
assert shopping_cart.get_total_quantity() == 0
assert shopping_cart.get_total_price() == 0
shopping_cart.add_item("item1", 10, 2)
assert shopping_cart.get_total_quantity() == 2
assert shopping_cart.get_total_price() == 20
shopping_cart.remove_item("item1")
assert shopping_cart.get_total_quantity() == 0
assert shopping_cart.get_total_price() == 0
```
4. 错误推测法:
```python
def test_database():
database = Database()
assert database.get_user("username") == None
database.add_user("username", "password")
assert database.get_user("username") == User("username", "password")
assert database.get_user("username2") == None
database.delete_user("username")
assert database.get_user("username") == None
```
5. 边界值和等价类组合法:
```python
def test_triangle():
assert get_triangle_type(1, 1, 1) == "equilateral"
assert get_triangle_type(2, 2, 1) == "isosceles"
assert get_triangle_type(200, 200, 1) == "isosceles"
assert get_triangle_type(3, 4, 5) == "scalene"
assert get_triangle_type(2147483647, 2147483647, 2147483647) == "equilateral"
assert get_triangle_type(2147483647, 2147483647, 1) == "isosceles"
assert get_triangle_type(2147483647, 1, 1) == "scalene"
assert get_triangle_type(0, 1, 2) == "invalid"
assert get_triangle_type(1, 1, -1) == "invalid"
assert get_triangle_type(1, 1, 2147483647) == "invalid"
```
6. 判定覆盖法:
```python
def test_search():
search_engine = SearchEngine()
search_engine.add_document("document1", "this is a test document")
search_engine.add_document("document2", "this is another test document")
assert search_engine.search("test") == ["document1", "document2"]
assert search_engine.search("another") == ["document2"]
assert search_engine.search("") == []
```
7. 边界值组合法:
```python
def test_calculator():
calculator = Calculator()
assert calculator.add(0, 0) == 0
assert calculator.add(2147483647, 1) == "Error: integer overflow"
assert calculator.divide(1, 0) == "Error: division by zero"
assert calculator.divide(2147483647, 2) == 1073741823
assert calculator.multiply(-2147483648, 2) == "Error: integer overflow"
assert calculator.subtract(0, 0) == 0
assert calculator.subtract(-2147483648, 1) == 2147483647
assert calculator.subtract(2147483647, -1) == -2147483648
assert calculator.subtract(1, 2147483647) == -2147483646
assert calculator.subtract(-2147483648, -1) == -2147483647
```
8. 边界值和误用情况组合法:
```python
def test_filesystem():
filesystem = Filesystem()
assert filesystem.create_file("test.txt") == "File created"
assert filesystem.create_file("test.txt") == "Error: file already exists"
assert filesystem.create_file("") == "Error: invalid filename"
assert filesystem.create_file(None) == "Error: invalid filename"
assert filesystem.rename_file("test.txt", "new_test.txt") == "File renamed"
assert filesystem.rename_file("test.txt", "new_test.txt") == "Error: file does not exist"
assert filesystem.rename_file("", "new_test.txt") == "Error: invalid filename"
assert filesystem.rename_file(None, "new_test.txt") == "Error: invalid filename"
assert filesystem.delete_file("new_test.txt") == "File deleted"
assert filesystem.delete_file("new_test.txt") == "Error: file does not exist"
```
9. 边界值和特殊值组合法:
```python
def test_string():
assert reverse_string("Hello, world!") == "!dlrow ,olleH"
assert reverse_string("") == ""
assert reverse_string(None) == None
assert reverse_string("H") == "H"
assert reverse_string("HH") == "HH"
assert reverse_string("HHH") == "HHH"
assert reverse_string("HHHH") == "HHHH"
assert reverse_string("HHHHH") == "HHHHH"
assert reverse_string("HHHHHH") == "HHHHHH"
assert reverse_string("HHHHHHH") == "HHHHHHH"
```
10. 随机测试法:
```python
def test_sort():
for i in range(10):
lst = [random.randint(-100, 100) for j in range(random.randint(0, 1000))]
sorted_lst = sorted(lst)
assert sort(lst) == sorted_lst
```
阅读全文