Python test and build

更新时间:
复制 MD 格式

Create a Flow pipeline that automates Python code checkout, build, unit testing, and test reporting. Detailed logs help you identify and resolve issues quickly.

Step 1: Create a test build pipeline

  1. Log on to the Alibaba Cloud DevOps console and click Create Pipeline in the upper-right corner.

  2. In the Choose a Template dialog box, select Python · Test, build , and then click Create .

Step 2: Configure the pipeline source

  1. In the Pipeline Source stage, click Add Pipeline Source.

  2. Select Sample Code Source. Set Code Type to Python and click Add.

Step 3: Configure the test stage

In the Test stage, configure tasks for different build clusters and build environments.

  1. Configure the basic parameters.

    Parameter

    Description

    Job Name

    Enter a custom name for the job, or use the default.

    Build Cluster

    The following build clusters are supported:

    Build Environment

    The following build environments are supported:

    • Specify Container Environment (for Alibaba Cloud DevOps default build cluster): Flow starts a specified container on the build machine and performs the build in that single-container environment. Alibaba Cloud DevOps provides a list of official images. You can also specify your own image as the runtime environment.

    • Default VM Environment (supported only for private build clusters): You must select Specify Build Nodes. Flow installs the environment and runs the job directly on the build machine. To speed up the job, we recommend installing the required SDKs and environment on the build machine in advance.

    • Default Environment (for Alibaba Cloud DevOps default/private build cluster): Flow uses different default container images based on the job type. These images come pre-installed with the necessary SDKs and environments and cannot be modified. (Deprecated)

    Note

    During job execution, the build environment is allocated to you. You have full control over the build environment and the job execution script.

    Download Pipeline Source

    If you enable this option, the configured code source is downloaded to the corresponding working directory. The available options are:

    • Download All Pipeline Sources

    • Do Not Download Pipeline Sources

    • Download Selected Pipeline Sources

  2. Configure the Python Unit Test task.

    Configure the test task based on your selected build environment.

    Specified container environment and Default VM environment

    1. Add an Install Python step. Set the Step Name to Install Python and select a Python version for your project, such as 3.7.

    2. Add an Execute Test Command step. In the Execute Command field, enter pip install pytest pytest-html -i https://mirrors.aliyun.com/pypi/simple to install test dependencies, then pytest --ignore=Python --html=report/index.html || true to run tests and generate an HTML report.

    3. In the Unit Test Report step, configure the following parameters.

      Parameter

      Description

      Test report file path

      Path to the test report file, such as report/index.html. The containing directory is uploaded.

      testing tool

      Testing tool or framework, such as Python-Pytest.

      Fail on Error

      If enabled, the pipeline stops when a unit test fails. This option is enabled by default.

    Default environment

    Note

    Pipelines created from templates use the specified container environment by default. If you switch to the Default environment, existing task steps become invalid. Clear the original configuration and add the following steps.

    Go to Add Step > Test > Python Unit Test and configure the parameters.

    Parameter

    Description

    Step name

    A custom name for the step.

    Python version

    Select a Python version based on your project requirements.

    Test command

    Custom command to run tests and generate reports. Runs from the code repository root and supports pytest-html reports.

    Test report directory

    Directory containing the generated test report, such as report. Automatically uploaded for display.

    Test report entry file

    Entry file for the report, such as index.html.

    Redline information

    Define the success and failure conditions for the task.

    The default test command is pytest --html=report/index.html. In Redline Information, select metrics such as Test Pass Rate and set a comparison condition and threshold (for example, greater than 100).

Step 4: Configure the build stage

In the Python build stage, you can configure build tasks for different build environments.

Specified container environment and Default VM environment

  1. In the Install Python step, select an appropriate Python version based on your build requirements.

  2. Add an Execute Python build command step. The default command is make build. In the Execute Command field, enter python --version to verify the Python version.

  3. In the Artifact upload task, configure the Upload method and related parameters.

    Select an upload method and configure its parameters.

    Private generic artifact repository

    Parameters:

    Parameter

    Description

    Add Service Connection

    Click Add Service Connection and follow the prompts to create a service connection from Flow to Packages. For more information, see Manage service connections.

    Repository

    After you add the service connection, you can select a Generic Artifacts repository in Packages as the target repository.

    Note

    For more information about Generic Artifacts repositories, see Manage generic artifacts.

    Artifact Name

    This name distinguishes artifacts from different builds and is used by deployment components. We recommend using dynamic variables. The default is Artifacts_${PIPELINE_ID}. You can specify a custom name, such as target1.

    Artifact Version

    The version uniquely identifies your artifact. You cannot push duplicate versions of the same artifact. We recommend using a dynamic variable, such as ${DATETIME}.

    Packaging Path

    Specify the relative path to the project's root directory, such as target/. You can specify multiple paths.

    Include packaging path in artifact

    If this option is selected, the generated archive includes the full packaging path. Otherwise, it only includes the files. If you specify multiple paths for Packaging Path, this option is automatically enabled.

    Public storage

    Parameters:

    Parameter

    Description

    Artifact Name

    This name distinguishes artifacts from different builds and is used by deployment components. We recommend using dynamic variables. The default is Artifacts_${PIPELINE_ID}. You can specify a custom name, such as target1.

    Packaging Path

    Specify the relative path to the project's root directory, such as target/. You can specify multiple paths.

    Include packaging path in artifact

    If this option is selected, the generated archive includes the full packaging path. Otherwise, it only includes the files. If you specify multiple paths for Packaging Path, this option is automatically enabled.

Default environment

Note

Pipelines created from templates use the specified container environment by default. If you switch to the Default environment, existing task steps become invalid. Clear the original configuration and add the following steps.

  1. Go to Add Step > Build > Python Build and configure the build commands.

    Parameter

    Description

    Step name

    A custom name for the step.

    Python version

    Select a Python version based on your project requirements.

    Execute build command

    The default command for Python is python --version.

    In Advanced Settings, Build Environment Specification defaults to DEFAULT and Timeout to 240 minutes.

  2. Go to Add Step > Upload > Artifact Upload and configure the artifact parameters.

    Select an upload method and configure its parameters.

    Private generic artifact repository

    Parameters:

    Parameter

    Description

    Add Service Connection

    Click Add Service Connection and follow the prompts to create a service connection from Flow to Packages. For more information, see Manage service connections.

    Repository

    After you add the service connection, you can select a Generic Artifacts repository in Packages as the target repository.

    Note

    For more information about Generic Artifacts repositories, see Manage generic artifacts.

    Artifact Name

    This name distinguishes artifacts from different builds and is used by deployment components. We recommend using dynamic variables. The default is Artifacts_${PIPELINE_ID}. You can specify a custom name, such as target1.

    Artifact Version

    The version uniquely identifies your artifact. You cannot push duplicate versions of the same artifact. We recommend using a dynamic variable, such as ${DATETIME}.

    Packaging Path

    Specify the relative path to the project's root directory, such as target/. You can specify multiple paths.

    Include packaging path in artifact

    If this option is selected, the generated archive includes the full packaging path. Otherwise, it only includes the files. If you specify multiple paths for Packaging Path, this option is automatically enabled.

    Public storage

    Parameters:

    Parameter

    Description

    Artifact Name

    This name distinguishes artifacts from different builds and is used by deployment components. We recommend using dynamic variables. The default is Artifacts_${PIPELINE_ID}. You can specify a custom name, such as target1.

    Packaging Path

    Specify the relative path to the project's root directory, such as target/. You can specify multiple paths.

    Include packaging path in artifact

    If this option is selected, the generated archive includes the full packaging path. Otherwise, it only includes the files. If you specify multiple paths for Packaging Path, this option is automatically enabled.

Step 5: Run the pipeline and check the results

  1. After the pipeline completes, the results page shows #2 Succeeded . The visualization displays the Pipeline Source (master branch), Test stage (Python unit test, 21s), and Build stage (Python build, 47s) with the output artifact.

  2. Click Logs on a task node to view its execution log. A green checkmark indicates success. For example, the artifact upload log confirms the artifact was uploaded.

  3. If you used the Organization-private generic artifact repository, find the artifact in Artifact Repository Packages. Generic artifact management.