用python写12个自动化测试的代码
时间: 2024-04-02 13:34:04 浏览: 87
python代码,自动化测试
好的,以下是12个用Python编写的自动化测试代码示例:
1. 测试网站是否正常运行
```python
import requests
def test_website_status():
response = requests.get('http://www.example.com')
assert response.status_code == 200
```
2. 测试登录功能
```python
from selenium import webdriver
def test_login():
driver = webdriver.Chrome()
driver.get('http://www.example.com/login')
username = driver.find_element_by_name('username')
password = driver.find_element_by_name('password')
submit_button = driver.find_element_by_id('submit')
username.send_keys('myusername')
password.send_keys('mypassword')
submit_button.click()
assert driver.current_url == 'http://www.example.com/home'
driver.quit()
```
3. 测试API功能
```python
import requests
def test_api():
response = requests.get('http://api.example.com/users')
assert len(response.json()) > 0
```
4. 测试数据库连接
```python
import psycopg2
def test_database_connection():
conn = psycopg2.connect(database='mydatabase', user='myuser', password='mypassword', host='localhost', port='5432')
cur = conn.cursor()
cur.execute('SELECT COUNT(*) FROM users')
result = cur.fetchone()
assert result[0] > 0
cur.close()
conn.close()
```
5. 测试邮件发送
```python
import smtplib
from email.mime.text import MIMEText
def test_email():
sender = 'myemail@example.com'
receiver = 'youremail@example.com'
message = MIMEText('This is a test email.')
message['Subject'] = 'Test Email'
message['From'] = sender
message['To'] = receiver
smtp_server = smtplib.SMTP('smtp.example.com', 587)
smtp_server.starttls()
smtp_server.login(sender, 'mypassword')
smtp_server.sendmail(sender, receiver, message.as_string())
smtp_server.quit()
```
6. 测试文件读写
```python
def test_file_io():
with open('test.txt', 'w') as f:
f.write('This is a test file.')
with open('test.txt', 'r') as f:
content = f.read()
assert content == 'This is a test file.'
```
7. 测试加密解密
```python
from cryptography.fernet import Fernet
def test_encryption():
key = Fernet.generate_key()
fernet = Fernet(key)
message = b'This is a secret message.'
encrypted_message = fernet.encrypt(message)
decrypted_message = fernet.decrypt(encrypted_message)
assert message == decrypted_message
```
8. 测试图像处理
```python
from PIL import Image
def test_image_processing():
image = Image.open('test.png')
grayscale_image = image.convert('L')
grayscale_image.save('test_grayscale.png')
assert grayscale_image.mode == 'L'
```
9. 测试网络连接
```python
import socket
def test_network_connection():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex(('www.example.com', 80))
assert result == 0
sock.close()
```
10. 测试正则表达式
```python
import re
def test_regular_expression():
pattern = r'\d+'
string = 'This is a test string with 123 numbers.'
matches = re.findall(pattern, string)
assert matches == ['123']
```
11. 测试时间处理
```python
import datetime
def test_datetime():
now = datetime.datetime.now()
tomorrow = now + datetime.timedelta(days=1)
assert tomorrow > now
```
12. 测试命令行执行
```python
import subprocess
def test_command_line_execution():
result = subprocess.run(['ls', '-l'], capture_output=True, text=True)
assert 'test.txt' in result.stdout
```
这些示例代码可以帮助您理解如何使用Python编写自动化测试用例。当然,具体的测试用例还要根据不同的需求来编写。
阅读全文