Realize WEB and API automated testing based on Docker


Computer configuration: Windows 10, Asus Tianxuan, R9-4900H, RTX2060


1. Install Docker


1. Enter the official website (https://www.docker.com/products/docker-desktop/), and install the Windows version of Docker.
2. Then it is a fool-like installation. After the installation is complete, enter docker version in CMD and an interface similar to the following will appear, indicating that the installation is successful.




Two, write Dockerfile


Write a Dockerfile file in the project directory to generate a custom docker image:

# 获取镜像markadams/chromium-xvfb-py3,该基本镜像支持chrome浏览器无头WEB测试(兼容API测试)
FROM markadams/chromium-xvfb-py3

# 设置时区,避免错误的日志时间造成困惑
ENV TZ 'Asia/Shanghai'

# 设置编码方式,将镜像中的默认编码方式设置为UTF-8
ENV LANG C.UTF-8

# 设置环境变量,将项目目录添加为系统环境变量
ENV PATH=$PATH:/code

# 安装字体,镜像中不支持中文显示,需要添加中文字体以支持显示,simsun.ttf可自行下载或替换
COPY ./resources/simsun.ttf /usr/share/fonts/ttf-dejavu/simsun.ttf

# 拷贝镜像源,使用国内镜像源避免安装依赖时超时
COPY ./resources/sources.list /etc/apt/sources.list

# 添加代码到镜像,将当前目录(即项目目录)的所有内容拷贝到镜像中的code目录下
COPY . /code

# 更新apt-get,以便获取最新版本列表
RUN apt-get update

# 安装pip,用于支持后续安装Python项目的依赖
RUN apt-get install -y python3-pip --allow-unauthenticated

# 安装依赖,requirements.txt需要包含项目需要的所有依赖
RUN pip3 install -r /code/requirements.txt

# 运行程序,基于该镜像创建容器时会执行以下命令,即运行自动化测试项目的入口程序
CMD ["python3", "/code/main/main.py"]

In the example, Alibaba Cloud’s domestic image sources are used, and the contents of sources.list are as follows:

deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

3. Execution steps


(During this period, it is necessary to maintain the running of the Docker-desktop program)

1. Start CMD as an administrator and enter the directory of the automated test project. The sample project directory is located at D:\temp\TestRepo.




2. Use the following command to create a mirror, which will create a custom mirror named autotest and version number v0.1 based on the Dockerfile, and use the current directory as the context path of the mirror.

docker build -t autotest:v0.1 .



3. Use the following command to view the list of mirrors that have been created.

docker images



4. Use the following command to create a container named autotest_container based on the above custom image, and the CMD command in the Dockerfile will be automatically run after creation.

docker run -it --name=autotest_container autotest:v0.1



The sample project includes two WEB automation use cases and four API automation use cases. The figure above shows that all six use cases are executed normally and passed.

5. Use the following command to view the list of existing containers.

docker ps -a



6. Use the following command to delete the image and container.

# 根据容器名(autotest_container)删除容器
docker rm autotest_container
# 根据镜像名(autotest:v0.1)删除镜像
docker rmi "autotest:v0.1"



The deleted containers and images no longer appear in the list of containers and images.




If there are mistakes or improvements, please actively point them out!

Guess you like

Origin blog.csdn.net/embracestar/article/details/127192662