Python test and build

Updated at:

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 Flow console. In the upper-right corner, click Create Pipeline.

  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

    You can specify a custom job name or use the default name.

    Build Cluster

    The following build clusters are supported:

    Build Environment

    The following build environments are supported:

    • Specify Container Environment (Alibaba Cloud DevOps default build cluster): Flow starts a specified container on the build machine and performs the build in that container. You can select from the provided Flow System Images or specify your own custom image as the runtime environment.

    • Default VM Environment (supported only for self-hosted build clusters): You must select a build node from the Specify Build Nodes list. Flow installs the required environment and runs the job directly on the build machine. To speed up the build process, we recommend that you install the necessary SDKs and environment dependencies on the machine beforehand.

    • Default Environment (Alibaba Cloud DevOps default build cluster/self-hosted build cluster): (Deprecated) Flow uses a default container image based on the job type. The image comes pre-installed with the required SDKs and environment, and cannot be changed.

    Note

    During job execution, the build environment is exclusively allocated to you. You have full control over the environment and can customize the execution scripts.

    Download Pipeline Source

    When enabled, the configured source files are downloaded to the working directory. The following options are available:

    • 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.

    Upload to Packages

    Parameters:

    Parameter

    Description

    Add Service Connection

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

    Repository

    After the service connection is created, select a Generic repository in Packages as the destination repository.

    Note

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

    Artifact Name

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

    Artifact Version

    The version uniquely identifies your artifact file. You cannot push an artifact with a duplicate version to the same repository. We recommend using a dynamic variable, such as ${DATETIME}.

    Package Path

    The path to the files or directories to package, relative to your project's root folder. For example, target/. You can specify multiple paths.

    Include package path in artifact

    If selected, the generated archive includes the full directory structure specified in Package Path. If cleared, the archive contains only the files. This option is automatically set to true if you specify multiple package paths.

    Archive to public storage

    Parameters:

    Parameter

    Description

    Artifact Name

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

    Package Path

    The path to the files or directories to package, relative to your project's root folder. For example, target/. You can specify multiple paths.

    Include package path in artifact

    If selected, the generated archive includes the full directory structure specified in Package Path. If cleared, the archive contains only the files. This option is automatically set to true if you specify multiple package paths.

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.

    Upload to Packages

    Parameters:

    Parameter

    Description

    Add Service Connection

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

    Repository

    After the service connection is created, select a Generic repository in Packages as the destination repository.

    Note

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

    Artifact Name

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

    Artifact Version

    The version uniquely identifies your artifact file. You cannot push an artifact with a duplicate version to the same repository. We recommend using a dynamic variable, such as ${DATETIME}.

    Package Path

    The path to the files or directories to package, relative to your project's root folder. For example, target/. You can specify multiple paths.

    Include package path in artifact

    If selected, the generated archive includes the full directory structure specified in Package Path. If cleared, the archive contains only the files. This option is automatically set to true if you specify multiple package paths.

    Archive to public storage

    Parameters:

    Parameter

    Description

    Artifact Name

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

    Package Path

    The path to the files or directories to package, relative to your project's root folder. For example, target/. You can specify multiple paths.

    Include package path in artifact

    If selected, the generated archive includes the full directory structure specified in Package Path. If cleared, the archive contains only the files. This option is automatically set to true if you specify multiple package paths.

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.