用python写12个自动化测试的代码

时间: 2024-04-02 08:34:04 浏览: 13
好的,以下是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编写自动化测试用例。当然,具体的测试用例还要根据不同的需求来编写。

相关推荐

最新推荐

recommend-type

Python+appium框架原生代码实现App自动化测试详解

主要介绍了Python+appium框架原生代码实现App自动化测试详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

Python+Selenium使用Page Object实现页面自动化测试

主要介绍了Python+Selenium使用Page Object实现页面自动化测试,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

接口自动化测试框架完整搭建python+unittest+requests+ddt

接口自动化框架 一、首先废话不多说,直接开始讲解一下整个框架的目录结构,本次内容着重讲解测试用例类,只要掌握了测试用例类和接口测试用例怎么写就可以运用此框架。(需要一些python基础,能看的懂基本代码) ...
recommend-type

python接口自动化测试框架结构 ( 第二章)-封装Excel方法

python接口自动化测试框架结构 ( 第一章) 点击跳转到第一章,你可以知道框架结构!! 前方高能!!正文开始!! 1、在utlis.py封装Excel读写方法 (文件位置跳转第一章看代码结构) 大家需要知道,读取Excel分两种...
recommend-type

Python自动化测试中yaml文件读取操作

主要介绍了Python自动化测试中yaml文件读取操作,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。