簡體   English   中英

在 Spock 測試中看不到痕跡

[英]Can't see traces in Spock test

我正在嘗試將跟蹤放入 Spock 測試中,以查看 PostgreSQL 容器是如何由測試容器創建的。 但無論是 log4j,還是標准 output 上的打印,也不是通過例程擴展規范,我都可以做到。 測試代碼如下::

package com.makobrothers.mako.rrhh.persona.infrastructure.controller

import com.makobrothers.mako.IntegrationTest
import com.makobrothers.mako.MakoApplication
import org.junit.experimental.categories.Category
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.ActiveProfiles
import org.springframework.test.context.DynamicPropertyRegistry
import org.springframework.test.context.DynamicPropertySource
import org.springframework.test.context.TestPropertySource
import org.testcontainers.containers.PostgreSQLContainer
import org.testcontainers.spock.Testcontainers
import spock.lang.Shared;
import spock.lang.Specification

import java.text.DateFormat
import java.text.SimpleDateFormat
import static org.hamcrest.Matchers.*
import org.json.simple.JSONObject


@ActiveProfiles("test")
@Testcontainers
@SpringBootTest(
    webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,
    classes = MakoApplication.class
)
@TestPropertySource(locations = "classpath:application-test.yml")
@Category(IntegrationTest.class)
//@Sql(scripts = ["classpath:/createTablePersona.sql", "classpath:/insertTablePersona.sql"])
class PersonaRestAssuredSpec extends Specification {
    final static Logger log = LoggerFactory.getLogger(PersonaRestAssuredSpec.class)
    final static POSTGRES_TEST_IMAGE = "postgres:15.1"
    final static Random RANDOM = new Random();
    public static PostgreSQLContainer staticPostgreSQL = new PostgreSQLContainer(POSTGRES_TEST_IMAGE)
            .withDatabaseName("makoTest")
            .withUsername("mako")
            .withPassword("mako")
            .withExposedPorts(5432)
            .withCreateContainerCmdModifier({ cmd -> cmd.withName('postgreSQL-' + ( RANDOM.nextInt() & Integer.MAX_VALUE )) })

    @Shared
    public PostgreSQLContainer postgreSQLContainer = staticPostgreSQL
      
    @Value('\${server.http.port}')
    private int port

    def setup() throws Exception {
        RestAssured.port = port
        log.info("Url: " + postgreSQLContainer.getJdbcUrl())
        log.warn("Url: " + postgreSQLContainer.getJdbcUrl())
        log.error("Url: " + postgreSQLContainer.getJdbcUrl())
        showTrace "Url: " + postgreSQLContainer.getJdbcUrl()
        showTrace "DatabaseName: " + postgreSQLContainer.getDatabaseName()
        showTrace "Username: " + postgreSQLContainer.getUsername()
        showTrace "Password: " + postgreSQLContainer.getPassword()
        showTrace "Port: " + postgreSQLContainer.getBoundPortNumbers()
    }
    
    def "showConfigTest"() {
        given: "A client request to test findById"
            log.info("Url: " + postgreSQLContainer.getJdbcUrl())
            log.warn("Url: " + postgreSQLContainer.getJdbcUrl())
            log.error("Url: " + postgreSQLContainer.getJdbcUrl())
            showTrace "Url: " + postgreSQLContainer.getJdbcUrl()
            int left = 2
            int right = 2
        when: "Nothing"
            int result = left + right
        then: "Nothing"
            assert result == 4
    }
    
}

我用打印方法 (SpockConfig.groovy) 擴展了 Spec,正如我在另一個查詢中看到的示例:

import spock.lang.Specification

class LabelPrinter {
    def showTrace(def message) {
        println message
        true
    }
}

Specification.mixin LabelPrinter

gradle 部分聲明為:

test {
    useJUnitPlatform()

    testLogging {
        events "PASSED", "SKIPPED", "FAILED", "STANDARD_OUT", "STANDARD_ERROR"
    }
    
    testClassesDirs = sourceSets.test.output
    classpath = sourceSets.test.runtimeClasspath

    reports {
        html.enabled = true
    }
}

output 總是一樣的:

$ gradle clean test

> Task :test

  com.makobrothers.mako.rrhh.persona.infrastructure.controller.PersonaRestAssuredSpec

    ✔ showConfigTest (5.5s)
  1 passing (1m 15s)


Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/7.6/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 1m 27s
6 actionable tasks: 6 executed

將此添加到 gradle,現在可以使用了:

testlogger {
    // pick a theme - mocha, standard, plain, mocha-parallel, standard-parallel or plain-parallel
    theme 'mocha'
    // set to false to disable detailed failure logs
    showExceptions true
    // set threshold in milliseconds to highlight slow tests
    slowThreshold 2000
    // displays a breakdown of passes, failures and skips along with total duration
    showSummary true
    // set to false to hide passed tests
    showPassed true
    // set to false to hide skipped tests
    showSkipped true
    // set to false to hide failed tests
    showFailed true
    // enable to see standard out and error streams inline with the test results
    showStandardStreams true
    // set to false to hide passed standard out and error streams
    showPassedStandardStreams true
    // set to false to hide skipped standard out and error streams
    showSkippedStandardStreams true
    // set to false to hide failed standard out and error streams
    showFailedStandardStreams true
}

test {
    useJUnitPlatform()

    testLogging {
        outputs.upToDateWhen {false}
        showStandardStreams = true
        events "PASSED", "SKIPPED", "FAILED", "STANDARD_OUT", "STANDARD_ERROR"
    }
    
    testClassesDirs = sourceSets.test.output
    classpath = sourceSets.test.runtimeClasspath

    reports {
        html.enabled = true
    }
}

我認為是 showStandardStreams 屬性允許查看日志。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM