github如何基于workflow实现自动集成测试提升代码质量

背景

之前研究过spring boot如何不依赖外部redis、mysql等中间件也不mock实现集成测试

这种做法主要是在本地手动进行集成测试,我们最终的目的肯定是希望能够实现自动化集成测试

所以今天我们就来看看如何通过github workflow+docker实现自动集成测试

添加集成测试相关maven依赖

添加maven依赖

首先我们需要添加集成测试相关的maven依赖

xml 复制代码
        <dependency>
            <groupId>org.testcontainers</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>1.17.6</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.testcontainers</groupId>
            <artifactId>testcontainers</artifactId>
            <version>1.20.4</version>
            <scope>test</scope>
        </dependency>

添加maven插件依赖

其次添加集成测试相关的maven插件依赖

xml 复制代码
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <includes>
                        <include>**/*Test.java</include> <!-- 包含所有以 Test 结尾的集成测试类 -->
                    </includes>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <classesDirectory>${project.build.outputDirectory}</classesDirectory>
                </configuration>
            </plugin>
            
        </plugins>
    </build>

编写github workflow

依赖添加完成后我们就需要编写github workflow

shell 复制代码
name: Integration Testing with Testcontainers
on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  integration-testing:
    name: Integration Testing
    runs-on: ubuntu-latest
    services:
      docker:
        image: docker:dind
        options: --privileged
    steps:
      # 检出代码
      - name: Check out the repo
        uses: actions/checkout@v2

      - name: Set up JDK 11
        uses: actions/setup-java@v2
        with:
          java-version: '11'
          distribution: 'adopt'

      # 缓存 Maven 依赖
      - name: Cache Maven Dependencies
        uses: actions/cache@v2
        with:
          path: ~/.m2/repository
          key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
          restore-keys: |
            ${{ runner.os }}-maven-

      # 运行集成测试
      - name: Run Integration Tests
        run: mvn verify -B -Dmaven.test.skip=false -Dgpg.skip=true

这里相关的源码可以参考多级缓存框架fluxcache-integration-test.yml

注意这个文件必须放在.github/workflows目录下

编写测试代码

java 复制代码
@SpringBootTest(classes = FluxCacheApplication.class)
@Slf4j
@Testcontainers
public class TestControllerTest {

    @Container
    public static GenericContainer<?> redis = new GenericContainer<>(DockerImageName.parse("redis:6.2.6"))
        .withExposedPorts(6379);

    @DynamicPropertySource
    static void redisProperties(DynamicPropertyRegistry registry) {
        registry.add("redis.host", redis::getHost);
        registry.add("redis.port", () -> redis.getMappedPort(6379).toString());
        registry.add("redis.password", () -> "");
    }

    private final static Long SLEEP_TIME = 3L;

    @Autowired
    private FluxCacheProperties cacheProperties;

    @Autowired
    private TestController testController;

    @Autowired
    private DefaultFluxCacheManager cacheManager;

    @Test
    public void testFirstCacheByCaffeine() {
        List<StudentVO> vos = testController.firstCacheByCaffeine("aaa");
        StudentVO vo = vos.get(0);
        int age = vo.getAge();
        List<StudentVO> vos1 = testController.firstCacheByCaffeine("aaa");
        StudentVO vo1 = vos1.get(0);
        int age1 = vo1.getAge();
        assertEquals(age, age1);
        List<StudentVO> vos2 = testController.firstCacheByCaffeine("bb");
        StudentVO vo2 = vos2.get(0);
        assertNotEquals(age, vo2.getAge());
    }
}

测试相关的代码解释我们可以参考之前的spring boot如何不依赖外部redis、mysql等中间件也不mock实现集成测试

实际效果

我们在main分支上切一个新分支出来,修改代码,再发起Pull requests,就可以看到github workflow自动执行了集成测试

我们看看集成测试的结果

可以看到集成测试通过了

这样只要我们的测试用例写的足够全面,我们就可以保证我们的代码质量,进行重大重构,只要测试用例通过,我们就可以放心的发布

总结

通过github workflow+docker实现自动集成测试,可以保证我们的代码质量,让我们的代码更加健壮

我们也无需手动执行,不用担心有人没有执行测试,导致改动的代码有bug

通过github workflow我们实现自动化集成测试非常简单,任何改动有了ci的检查不会像以前没有底气。

至少能保证基本的功能是正常的,不会出现非常明显的bug

相关推荐
i***13248 小时前
Spring BOOT 启动参数
java·spring boot·后端
IT_Octopus8 小时前
(旧)Spring Securit 实现JWT token认证(多平台登录&部分鉴权)
java·后端·spring
kk哥88999 小时前
Spring详解
java·后端·spring
S***26759 小时前
Spring Cloud Gateway 整合Spring Security
java·后端·spring
码事漫谈9 小时前
C++单元测试框架选型与实战速查手册
后端
OneLIMS9 小时前
Windows Server 2022 + IIS + ASP.NET Core 完整可上传大文件的 报错的问题
windows·后端·asp.net
码事漫谈9 小时前
C++ 依赖管理三剑客:vcpkg、Conan、xmake 速查手册
后端
Tao____9 小时前
开源物联网平台
java·物联网·mqtt·开源·设备对接
计算机毕设匠心工作室9 小时前
【python大数据毕设实战】青少年抑郁症风险数据分析可视化系统、Hadoop、计算机毕业设计、包括数据爬取、数据分析、数据可视化、机器学习
后端·python
计算机毕设小月哥9 小时前
【Hadoop+Spark+python毕设】智能制造生产效能分析与可视化系统、计算机毕业设计、包括数据爬取、Spark、数据分析、数据可视化、Hadoop
后端·python·mysql