代码之家  ›  专栏  ›  技术社区  ›  dot

作为docker构建的一部分,dotnet测试在容器中失败,但在手动运行时有效

  •  0
  • dot  · 技术社区  · 4 年前

    我正试图从Dockerfile运行dotnet测试,但当我这样做时,它失败了,并出现以下错误:

    => [7/9] RUN npm install -g azure-functions-core-tools@4 --unsafe-perm true                                                                                                                     28.2s 
     => [8/9] RUN cd /builds/tests                                                                                                                                                                    0.5s 
     => ERROR [9/9] RUN dotnet test --test-adapter-path:. --logger:"junit;LogFilePath=..\TestResults\test-results.xml;MethodFormat=Class;FailureBodyFormat=Verbose"                                   0.5s 
    ------
     > [9/9] RUN dotnet test --test-adapter-path:. --logger:"junit;LogFilePath=..\TestResults\test-results.xml;MethodFormat=Class;FailureBodyFormat=Verbose":
    #13 0.508 MSBUILD : error MSB1003: Specify a project or solution file. The current working directory does not contain a project or solution file.
    ------
    executor failed running [/bin/sh -c dotnet test --test-adapter-path:. --logger:"junit;LogFilePath=..\TestResults\test-results.xml;MethodFormat=Class;FailureBodyFormat=Verbose"]: exit code: 1
    

    但是在/builds/tests文件夹中有一个测试项目(csproj)文件。以下是运行容器中该文件夹的输出:

    root@00b0dd66f0a7:/builds/tests# ls -lah
    total 80K
    drwxr-xr-x 1 root root 4.0K Apr 18 19:20 .
    drwxr-xr-x 1 root root 4.0K Apr 19 18:20 ..
    -rwxr-xr-x 1 root root  690 Mar 30 18:01 AzureServiceBusQueueTestClient.cs
    -rwxr-xr-x 1 root root  429 Mar 30 18:01 AzureStorageQueueTestClient.cs
    -rwxr-xr-x 1 root root 1.6K Mar 30 18:01 AzureStorageTableEnqueuedTestClient.cs
    -rwxr-xr-x 1 root root  887 Mar 30 18:01 ListLogger.cs
    -rwxr-xr-x 1 root root  116 Mar 30 18:01 LoggerTypes.cs
    -rwxr-xr-x 1 root root  259 Mar 30 18:01 NullScope.cs
    -rwxr-xr-x 1 root root 2.6K Apr 18 19:11 widgetStorageRequestsShould.cs
    -rwxr-xr-x 1 root root   88 Mar 30 18:01 README.md
    -rwxr-xr-x 1 root root 6.0K Apr 18 18:05 TestFactory.cs
    -rwxr-xr-x 1 root root 2.1K Apr 18 19:11 widgetRequestsShould.cs
    drwxr-xr-x 1 root root 4.0K Apr 18 19:20 bin
    drwxr-xr-x 1 root root 4.0K Apr 19 18:20 obj
    -rwxr-xr-x 1 root root 1.2K Mar 30 18:01 widgets-tests.csproj
    

    但是,如果我在构建容器后手动运行它,它可以正常工作:

    # cd /builds/tests/
    # bash
    root@00b0dd66f0a7:/builds/tests# dotnet test --test-adapter-path:. --logger:"junit;LogFilePath=..\TestResults\test-results.xml;MethodFormat=Class;FailureBodyFormat=Verbose"
      Determining projects to restore...
      Restored /builds/src/widgets.csproj (in 17.16 sec).
      Restored /builds/tests/widgets-tests.csproj (in 17.85 sec).
    
     ....
    Test run for /builds/tests/bin/Debug/net6.0/widgets-tests.dll (.NETCoreApp,Version=v6.0)
    Microsoft (R) Test Execution Command Line Tool Version 17.1.0
    Copyright (c) Microsoft Corporation.  All rights reserved.
    
    Starting test execution, please wait...
    A total of 1 test files matched the specified pattern.
    widgets File: /builds/TestResults/test-results.xml
    
    Passed!  - Failed:     0, Passed:     3, Skipped:     0, Total:     3, Duration: 23 ms - /builds/tests/bin/Debug/net6.0/widgets-tests.dll (net6.0)
    root@00b0dd66f0a7:/builds/tests#
    

    Dockerfile

    这就是我当前的Dockerfile的样子:

    FROM --platform=linux/amd64 mcr.microsoft.com/dotnet/sdk:6.0
    RUN apt update && apt-get install vim -y
    COPY . /builds
    RUN curl -sL https://aka.ms/InstallAzureCLIDeb | bash
    RUN apt-get install curl && curl -sL https://deb.nodesource.com/setup_12.x | bash -
    RUN apt-get install nodejs
    RUN npm install -g azure-functions-core-tools@4 --unsafe-perm true
    RUN cd /builds/tests
    # RUN dotnet test --test-adapter-path:. --logger:"junit;LogFilePath=..\TestResults\test-results.xml;MethodFormat=Class;FailureBodyFormat=Verbose"
    

    我确信我只是错过了一些简单的东西,但我似乎找不到。任何提示都将不胜感激。

    0 回复  |  直到 4 年前
        1
  •  0
  •   Def Soudani    4 年前
    RUN cd /builds/tests \
        && dotnet test ..
    

    WORKDIR /builds/tests 而不是 RUN cd /builds/tests

    推荐文章