0.前言
如果可以的话,请先关注(专栏和账号),然后点赞和收藏,最后学习和进步。你的支持是我继续写下去的最大动力,个人定当倾囊而送,不负众望。谢谢!!!
1.前提
基于win10专业版64位系统+64位jdk1.8+64位python3.6.5+社区版pycharm2018.1.3+unittest+selenium3.141.0。
要学好自动化测试,我们先从python语言基础开始学习,一步一个脚印,欲速则不达哦!
2.随机抽人项目
1)项目需求
a.所有人名存放在txt文件中;b.每次随机抽取一个不重复(即没有抽过的)人名并展示;c.所有人抽过之后给出选择提示,是继续下一轮从新抽取还是结束程序运行。
2)界面原型
3)业务流程图
4)无界面源码
read_file.py文件:
class ReadFile:def read_txt(self,filename):with open(filename,'r') as f:result =f.read().splitlines()return resultif __name__ == '__main__':result = ReadFile().read_txt("../data/students.txt")print(result)
choose_poople.py文件:
from test1903.RandomChoose.method.read_file import ReadFile
from random import randintclass ChoosePeople:def choosePeolpe(self):rf = ReadFile()list1 = rf.read_txt("./data/students.txt")#存储txt文件当中的所有学生姓名list2 = []#用来存储已经抽过的学员,还可以用来判断随机抽的学员是否抽过了while True:choose = input("1.随机选择一人,0.退出系统,请选择:")if choose == '1':if len(list2) != len(list1):while True:index = randint(0,len(list1) - 1)#获取一个随机下标if list1[index] not in list2:#盘对学员是否抽过list2.append(list1[index])#将没有抽过的学员加入list2print("幸运儿是:" + list1[index])#获得一个随机下标来表示一个随机学员姓名breakelse:continueelse:print("所有学生都抽过了!")list2.clear()while True:choose2 = input("1.重新开始,0.退出系统,请选择:")if choose2 == '1':breakelif choose2 == '0':exit()else:print("输入有误,请输入1或0!")continue#可加可不加,因为下面没有代码了elif choose == '0':exit()#退出结束程序运行else:print("您的输入有误!")
if __name__ == '__main__':cp = ChoosePeople()cp.choosePeolpe()
运行效果:
5)开发界面
代码写的再好,没有界面总感觉少点儿什么。。。。。。
打开QtDesigner设计界面:
保存后生成一个*.ui文件。
选中ui文件,然后点pyUIC自动生成界面模块文件:
项目目录结构:
源码贴出来,大家可以参考下:
# -*- coding: utf-8 -*-# Form implementation generated from reading ui file 'people.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!from PyQt5 import QtCore, QtGui, QtWidgetsclass Ui_Dialog(object):def setupUi(self, Dialog):Dialog.setObjectName("Dialog")Dialog.resize(279, 242)self.textBrowser_result = QtWidgets.QTextBrowser(Dialog)self.textBrowser_result.setGeometry(QtCore.QRect(10, 10, 256, 192))font = QtGui.QFont()font.setFamily("华文行楷")font.setPointSize(12)self.textBrowser_result.setFont(font)self.textBrowser_result.setObjectName("textBrowser_result")self.pushButton_start = QtWidgets.QPushButton(Dialog)self.pushButton_start.setGeometry(QtCore.QRect(40, 210, 61, 21))self.pushButton_start.setObjectName("pushButton_start")self.pushButton_close = QtWidgets.QPushButton(Dialog)self.pushButton_close.setGeometry(QtCore.QRect(180, 210, 61, 21))self.pushButton_close.setObjectName("pushButton_close")self.retranslateUi(Dialog)QtCore.QMetaObject.connectSlotsByName(Dialog)def retranslateUi(self, Dialog):_translate = QtCore.QCoreApplication.translateDialog.setWindowTitle(_translate("Dialog", "幸运儿"))self.pushButton_start.setText(_translate("Dialog", "开始"))self.pushButton_close.setText(_translate("Dialog", "关闭"))
程序入口执行文件源码:
from test1903.RandomChoose.UI.people import Ui_Dialog
import sys
from threading import Thread
from PyQt5 import QtWidgets, QtGui
from test1903.RandomChoose.method.read_file import ReadFile
from random import randintclass main_class(QtWidgets.QMainWindow,Ui_Dialog):def __init__(self):rf = ReadFile()self.name_list1 = rf.read_txt('./data/students.txt') # 获取所有学生姓名,以列表的形式返回给name_list1self.name_list2 = [] # 用于存储抽过的人QtWidgets.QMainWindow.__init__(self)Ui_Dialog.__init__(self)# 添加背景palette = QtGui.QPalette()icon = QtGui.QPixmap(r'./UI/bg.png')palette.setBrush(self.backgroundRole(), QtGui.QBrush(icon))self.setPalette(palette)self.setupUi(self)self.begin()#开始按钮def start_button(self):self.pushButton_start.clicked.connect(self.random_people)def random_people(self):if len(self.name_list1) != len(self.name_list2):while True:index = randint(0,len(self.name_list1) - 1)#随机获取一个学生的下标if self.name_list1[index] not in self.name_list2:self.name_list2.append(self.name_list1[index])self.textBrowser_result.clear()#先清空self.textBrowser_result.append("幸运儿是:%s" %(self.name_list1[index]))#在空间中显示对应的抽取结果breakelse:continueelse:self.textBrowser_result.clear() # 先清空self.textBrowser_result.append("所有学生都抽过了!") # 在空间中显示对应的抽取结果self.name_list2.clear()#关闭按钮def close_button(self):self.pushButton_close.clicked.connect(self.close)def begin(self):t1 = Thread(target=self.start_button)t1.setDaemon(True)t1.start()Thread(target=self.close_button).start()if __name__ == '__main__':# 每一pyqt5应用程序必须创建一个应用程序对象。sys.argv参数是一个列表,从命令行输入参数。app = QtWidgets.QApplication(sys.argv)dialog = main_class()#实例化# 显示在屏幕上dialog.show()# 系统exit()方法确保应用程序干净的退出sys.exit(app.exec())
运行效果:
说明:本章节掌握之后,大家就可以自行开发一些简单的带界面的测试辅助小工具了,满满的收获感哈!