I have started to use docker recently and I containerised a python application. I used the following Dockerfile.
FROM python:3.6.5
# working directory
WORKDIR /usr/src/app
# copy requirement file to working directory
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
ENTRYPOINT ["python", "./run.py"]
I have some unit test which I invoke using pytest. I was thinking how I can include this in the docker container such that unit tests would run inside a docker container. Should I build a different image for it. What are your recommendations on it.
Running tests against a browser in a Docker container through an Open Agent that is running in another Docker container. The browser and the Open Agent communicate with each other through a Docker network. The test executor is running on your local Windows machine. Running tests entirely from Docker containers.
You need a test environment that is identical to the container environment where you will run the code for production (use the same image i.e. ). It is fine to run tests on the image after deployment. I wouldn't include them as part of the dockerfile. Apply tests to that environment and when they pass tag the build.
Running unit tests inside a Docker container is more or less the same as building a project. First, I copy all my test projects inside the container using the COPY command: After copying, I execute dotnet restore on all test projects. RUN dotnet restore "Tests/CustomerApi.Test/CustomerApi.Test.csproj"
As we saw above, the Docker run command of the test container, returns a status code of 0 (tests passed) or 1 (something went wrong). The meaning of this is that we can use this technique in a CI building system. Here is an example for a script in a Jenkins job that builds the image, tests it, and pushes it if the test passed.
RUN npm install RUN npm run test This Dockerfile is easy to run like this docker build -f frontend.Dockerfile -t ng-test-docker . After running the Docker container tests, you would get a console output similar to the one below.
Now to run our application, we need to build and run it, like this: Now to run our tests, we just pass sh -c "TEST_COMMAND" after the docker run command. Like this: Note that “go test” is for Golang, adapt it for your language. The sh -c let you run any command inside your container. If you a have a CMD or entrypoint setup, it will overwrite it.
You can give
RUN python -m unittest test_file_name.py
or
RUN python -m unittest tests/*
If you have a name of your running container,
you can execute a command on the running container with docker execute
So let's say you want to run some tests through pytest
specifying path to tests
on a container called/labeled "my_container":
docker exec my_container pytest ./tests/superstuff/
Note 2 things:
./
path is relative to your WORKDIR
--user
endpoint like so:docker exec --user myuser:mygroup my_container pytest ./tests/superstuff/
At least on Linux(Ubuntu), you can also use $(id -u):$(id -g)
- to get id&default group of the user running the command
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With