import pytest import allure import logging from utils.mysql_database_connection_utils import MysqlDBUtil class TestChaxun(): def mysql_connect(self): self.conn = MysqlDBUtil # def teardown_class(self): # self.conn.sql_close def test_chaxun(self): data=self.conn.query("SELECT * from stu WHERE name = '张三' ") print(data)
时间: 2024-04-28 16:21:06 浏览: 13
根据你的代码,我猜测你正在使用 pytest 和 allure 进行测试,并且你的测试代码中包含一个 TestChaxun 类,其中有一个名为 test_chaxun 的方法。这个方法中调用了一个叫做 mysql_connect 的方法,来连接 MySQL 数据库,并使用 MysqlDBUtil 类来进行数据库操作。在 test_chaxun 中,你使用了 query 方法来执行一个 SQL 查询,返回了符合条件的所有记录,并将结果打印出来。
不过,你的代码中有一个问题,就是在 mysql_connect 方法中,你没有实例化 MysqlDBUtil 类,而是直接将类名作为参数传递给了 conn 变量。你需要将这个变量赋值为 MysqlDBUtil 的实例,才能正确地连接数据库并进行操作。
相关问题
import pymysql class MysqlDBUtil(): # 字典输出 def __init__(self, host, user, password, database, charset): # host = "localhost" # 指定访问的服务器,本地服务器指定“localhost”,远程服务器指定服务器ip地址 # port = 3306 # 指定端口号,范围在0-65535 # user = "root" # 用户名 # password = "123456" # 密码 # database = "sys" # 数据库的名字 # charset = "utf8" # 数据库的编码方式 # 建立连接 self.connect = pymysql.Connect(host=host, port=3306, user=user, passwd=password, db=database, charset=charset) # 数据库的查询 def query(self , sql): cursor = None res = None try: cursor = self.connect.cursor() cursor.execute(sql) res = cursor.fetchall() except Exception as e: print(str(e)) finally: cursor.close() return res # 数据库的增删改 def uid(self , sql): cursor = None try: cursor = self.connect.cursor() cursor.execute(sql) self.connect.commit() except Exception as e: print(str(e)) self.connect.rollback() finally: cursor.close() #数据库的关闭 def sql_close(self): self.connect.close() import pytest import allure import logging from utils.mysql_database_connection_utils import MysqlDBUtil import utils.mysql_database_connection_utils class TestChaxun(): def mysql_connect(self): self.conn = MysqlDBUtil("localhost", "root", "123456", "sys", "utf8") def teardown_class(self): self.conn.sql_close() def test1(self): data = self.conn.query("SELECT * from stu WHERE name = '张三' ") print(data)
这段代码中的问题可能是在TestChaxun类中的mysql_connect方法中,没有使用self关键字来定义conn属性。应该将mysql_connect方法中的 "self.conn" 更改为 "self.db",因为在类的初始化方法中已经定义了 "self.connect"。修改后的代码如下:
```
class TestChaxun():
def mysql_connect(self):
self.db = MysqlDBUtil("localhost", "root", "123456", "sys", "utf8")
def teardown_class(self):
self.db.sql_close()
def test1(self):
data = self.db.query("SELECT * from stu WHERE name = '张三' ")
print(data)
```
另外,你需要确保在运行测试之前已经安装了pytest和allure模块,并且将utils.mysql_database_connection_utils模块添加到PYTHONPATH中,以便测试代码能够正确地导入MysqlDBUtil类。
import pytest import pymysql from utils.mysql_database_connection_utils import MysqlDBUtil class TestChaxun: data = [] def setup_method(self): self.mysql_connect() def teardown_method(self): if hasattr(self, "db") and self.db is not None: self.db.sql_close() def mysql_connect(self): self.db = MysqlDBUtil("localhost", "root", "123456", "sys", "utf8") def chaxun_data(self): assert self.db is not None data1 = self.db.query("SELECT zjhm from user") for i in range(0,1): sql = f"SELECT xingming FROM user where zjhm = '{data1[i][0]}'" TestChaxun.data = self.db.query(sql) from testcases.test_mysql_chaxun import TestChaxun class CS: data1 = TestChaxun.data print(type(data1))优化一下这段代码 并且给出优化代码
这段代码的优化可以从以下几个方面入手:
1. 在 setup_method 方法中创建数据库连接对象,并在 teardown_method 方法中关闭连接,可以保证每个测试用例执行前都会创建数据库连接,执行后都会关闭连接,避免了连接对象没有释放的情况;
2. 在 chaxun_data 方法中,可以将第一个 for 循环的范围改为 data1 列表的长度,这样可以查询到所有 zjhm 对应的 xingming,而不仅仅是第一个;
3. 将 TestChaxun 类中的 data 属性改为实例属性,并在 chaxun_data 方法中使用 self.data 来引用,这样可以避免在类之间共享 data 属性的问题;
4. 将 TestChaxun 类和 CS 类分开定义,这样可以更好地组织代码结构。
优化后的代码如下所示:
```
import pytest
import pymysql
from utils.mysql_database_connection_utils import MysqlDBUtil
class TestChaxun:
def setup_method(self):
self.db = MysqlDBUtil("localhost", "root", "123456", "sys", "utf8")
def teardown_method(self):
if hasattr(self, "db") and self.db is not None:
self.db.sql_close()
def chaxun_data(self):
assert self.db is not None
data1 = self.db.query("SELECT zjhm from user")
self.data = []
for i in range(len(data1)):
sql = f"SELECT xingming FROM user where zjhm = '{data1[i][0]}'"
self.data.append(self.db.query(sql))
class TestChaxunCase:
def test_case(self):
test_chaxun = TestChaxun()
test_chaxun.chaxun_data()
assert isinstance(test_chaxun.data, list)
class CSCase:
def test_case(self):
test_chaxun = TestChaxun()
test_chaxun.chaxun_data()
data1 = test_chaxun.data
assert isinstance(data1[0], tuple)
```
优化后的代码中,使用了 pytest 的测试用例和测试方法,分别对 TestChaxun 类和 CS 类中的方法进行测试。在 TestChaxun 类中,每个测试方法都会创建一个 TestChaxun 实例,并在执行 chaxun_data 方法后,断言 data 属性的类型为 list。在 CSCase 类中,也会创建一个 TestChaxun 实例,并在执行 chaxun_data 方法后,将 data 属性赋值给 data1 变量,并断言 data1 的第一个元素的类型为 tuple。