一个python自动化测试脚本示例
时间: 2023-10-01 11:07:49 浏览: 43
以下是一个基本的Python自动化测试脚本示例:
```python
import unittest
class TestStringMethods(unittest.TestCase):
def test_upper(self):
self.assertEqual('hello'.upper(), 'HELLO')
def test_isupper(self):
self.assertTrue('HELLO'.isupper())
self.assertFalse('Hello'.isupper())
def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
# check that s.split fails when the separator is not a string
with self.assertRaises(TypeError):
s.split(2)
if __name__ == '__main__':
unittest.main()
```
这个脚本定义了一个名为`TestStringMethods`的测试类,其中包含三个测试方法:
- `test_upper`:测试字符串的`upper()`方法是否按预期工作。
- `test_isupper`:测试字符串的`isupper()`方法是否按预期工作。
- `test_split`:测试字符串的`split()`方法是否按预期工作。
在每个测试方法中,使用`assert`语句来断言测试是否通过。当测试失败时,`assert`语句将引发一个异常。
最后,使用`unittest.main()`函数来运行测试。