如何在Docker容器中运行unit testing

我创build了一个运行我的angular度项目的docker容器,现在我试图在容器内运行我的unit testing失败。 我需要一个无头浏览器来运行我的testing,PhantomJS对于我的口味来说太麻烦了,在运行testing时也会给Chrome带来不同的结果。

在这里,我提供了我的Dockerfile:

# download (or use if it's in cache) the latest official image from node FROM node:latest # create directory in the container and set all privileges RUN mkdir -p /usr/src/app && chmod 777 /usr/src/app # make the directory available for following commands WORKDIR /usr/src/app # copy all local's frontend content to the WORKDIR COPY . /usr/src/app # Expose the port the app runs in EXPOSE 4200 CMD ["npm", "start"] 

我尝试使用无头Chrome,但仍然需要一些更多的configuration,我不知道如何去做。 任何人有任何想法?

经过大量的调查,我find了一个办法做到这一点:

我在我的前端Dockerfile中安装了Chrome:

 RUN wget -q -O - https://dl.google.com/linux/linux_signing_key.pub | apt-key add - RUN echo 'deb http://dl.google.com/linux/chrome/deb/ stable main' >> /etc/apt/sources.list RUN apt-get update && apt-get install --no-install-recommends -y google-chrome-stable 

我使用karma.config中的适当configuration对我的testing使用了无头Chrome:

 browsers: ['Chrome_without_sandbox'], customLaunchers: { Chrome_without_sandbox: { base: 'ChromeHeadless', flags: ['--no-sandbox'] // with sandbox it fails under Docker } }, 

如果您使用selenium,我build议您创build一个不同的容器,seleniumtesting将被执行,我们将这个selenium-container称为selenium-container 。 由于您正在使用Node,因此您可以使用现有的selenium webdriver来编写unit testing。

除了那个容器,你可以使用chrome独立服务器作为无头浏览器,这样你就可以从selenium容器中执行你的unit testing,我们将这个chrome-container称为。

在编写unit testing时,可以通过连接到如下的无头浏览器服务器来开始testing:

 var driver = new webdriver.Builder() .forBrowser('chrome') .usingServer('http://localhost:4444/wd/hub') 

这只是为了让你开始,API是有据可查的,并易于使用,以编写你的testing。

将Angular项目中的unit testing隔离开来,这样不会有任何干扰。 开始testing可以通过使用docker撰写的一个命令完成。 您docker-compose.yml可能如下所示:

 version: '3' services: angular-app: build: context: . dockerfile: Dockerfile selenium_container: build: context: . dockerfile: Dockerfile.selenium.test depends_on: - chrome-container - angular-app chrome-container: image: selenium/standalone-chrome:3.4.0-einsteinium