一、报错信息:
(1)文件被占用
[2019-12-18 14:18:25,165] admin-PC/ERROR/stderr: Traceback (most recent call last):
File "d:\programs\python\python3.8\lib\site-packages\locust\core.py", line 394, in run
self.execute_next_task()
File "d:\programs\python\python3.8\lib\site-packages\locust\core.py", line 424, in execute_next_task
self.execute_task(task["callable"], *task["args"], **task["kwargs"])
File "d:\programs\python\python3.8\lib\site-packages\locust\core.py", line 436, in execute_task
task(self, *args, **kwargs)
File "G:\PycharmProjects\Locust_iwebshop_per\WorkFlow_Script\login_to_search.py", line 36, in search
os.remove(file_res)
PermissionError: [WinError 32] 另一个程序正在使用此文件,进程无法访问。: 'SearchOutputdata.csv'
(2)原代码
def search(self):
file = open("Searchdata.csv","r")
table = csv.reader(file)
file_res = "search_output.csv"
file_in = os.path.exists(file_res)
if file_in:
os.remove(file_res)
file2 = open("search_output.csv","a")
for row in table:
keyword = row
print(keyword)
url = "/index.php?........&word="+str(keyword)
response = self.client.get(url).text
res = response.find(str(keyword))
time1 = datetime.datetime.now() # 获取当前系统时间
if res>=0:
res = str(keyword)+"查询成功、测试通过"+str(time1)
file2.write(res+"\n")
else:
res = str(keyword)+"查询失败、测试未通过" + str(time1)
file2.write(res + "\n")
file2.close()
二、原因
因为并发造成的错误,同一个文件不能被同时写入打开,可以被同时读取。
代码中有这样的逻辑,需要先判断这个测试报告文件是否存在,如果存在,就进行删除,如果不存在,就创建文件进行写测试报告。
但是因为性能测试需要多个用户进行操作,如果有个用户正在进行写入,而另一个用户正好需要判断文件是否存在,这是后就会出现这个问题。
所以:
在自动化的功能和接口等非性能测试中可以使用这个文件是否存在的判断,但是在性能测试中,用户有并发操作,因此不适合这种逻辑
三、解决
(1)可以结合web自动化写入数据库的技术,把测试报告信息写入数据库表中,这个问题就可以解决了。
(2)如果确实需要判断,写测试报告,就可以将这个判断文件是否存在的代码放在WebsiteUser类中进行,而且要放在task_set之前