Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running application unit test inside a Docker container

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.

like image 425
CyberPunk Avatar asked Jan 08 '20 09:01

CyberPunk


People also ask

How do I run a test in a Docker container?

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.

Should Dockerfile run tests?

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.

How do I run unit tests inside a docker container?

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"

What does the Docker Run command mean?

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.

How do I run NPM tests in Docker?

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.

How to run a Golang application from a docker container?

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.


2 Answers

You can give

RUN python -m unittest test_file_name.py

or

RUN python -m unittest tests/*
like image 180
jassim Avatar answered Oct 09 '22 21:10

jassim


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:

  1. The ./ path is relative to your WORKDIR
  2. If tests are generating some test/output files, they may have docker-root owner instead of the regular one, if that's not what you want, you can override it with the --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

like image 23
jave.web Avatar answered Oct 09 '22 20:10

jave.web