Alv*_*nti 4 python tdd pycharm docker visual-studio-code
不要误会我的意思,virtualenv(或 pyenv)是一个很棒的工具,虚拟环境的整个概念是对开发人员环境的巨大改进,减轻了整个Snowflake Server反模式。
但是现在 Docker 容器无处不在(有充分的理由),让您的应用程序在容器上运行但同时在 IDE 中设置本地虚拟环境以运行测试等感觉很奇怪。
我想知道是否有办法利用 Docker 容器来实现这个目的?
是的,有一种方法可以实现这一目标。通过配置远程 Python 解释器和“sidecar”Docker 容器。
这个 Docker 容器将具有:
这里的想法是复制应用程序的容器并为其添加 SSH 功能。我们将使用docker-compose来实现这一点:
version: '3.3'
services:
dev:
build:
context: .
dockerfile: Dockerfile.dev
ports:
- 127.0.0.1:9922:22
volumes:
- .:/code/
environment:
DEV: 'True'
env_file: local.env
Run Code Online (Sandbox Code Playgroud)
FROM python:3.7
ENV PYTHONUNBUFFERED 1
WORKDIR /code
# Copying the requirements, this is needed because at this point the volume isn't mounted yet
COPY requirements.txt /code/
# Installing requirements, if you don't use this, you should.
# More info: https://pip.pypa.io/en/stable/user_guide/
RUN pip install -r requirements.txt
# Similar to the above, but with just the development-specific requirements
COPY requirements-dev.txt /code/
RUN pip install -r requirements-dev.txt
# Setup SSH with secure root login
RUN apt-get update \
&& apt-get install -y openssh-server netcat \
&& mkdir /var/run/sshd \
&& echo 'root:password' | chpasswd \
&& sed -i 's/\#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
Run Code Online (Sandbox Code Playgroud)
现在您可以直接从您的 IDE 运行您的测试,使用它来尝试测试驱动开发!它的关键点之一是快速反馈循环,不必等待完整的测试套件完成执行就可以查看您的新测试是否通过,这很棒!只需编写并立即运行它!
这个答案的内容也可以在这个GIST 中找到。
| 归档时间: |
|
| 查看次数: |
1230 次 |
| 最近记录: |