Home  >  Article  >  Backend Development  >  How to manage initialization code for test dependencies

How to manage initialization code for test dependencies

WBOY
WBOYforward
2024-02-09 18:00:15628browse

How to manage initialization code for test dependencies

php editor Xinyi will introduce you how to manage the initialization code of test dependencies. When developing software, you often need to use various testing tools and frameworks, and these tools and frameworks may require some initialization code for configuration and preparation. The way you manage this initialization code is important for the maintainability and scalability of your tests. The following will introduce you in detail how to manage the initialization code of test dependencies to help you better perform software testing work.

Question content

I have a logging wrapper (implemented using logrus below), and I'm using this package to log my application. The way I do this is by passing the logger variable to each required package via dependency injection.

Here, when writing test cases for each package, I have to write some code to initialize the logger package. How can we avoid writing initialization code for every test case of a package that uses a logger?

Logger/log.go

type logger interface {
    info(args ...interface{})
    infof(format string, keyvals ...any)
    infowithfields(fields map[string]interface{}, msg ...interface{})

    debug(args ...interface{})
    debugf(format string, keyvals ...any)
    debugwithfields(fields map[string]interface{}, msg ...interface{})
}

app_test.go

func setupLogrusLogger() (*bytes.Buffer, Logger) {
    buf := &bytes.Buffer{}
    logrusLogger := log.New()
    logrusLogger.Out = buf
    logrusLogger.Formatter = &log.JSONFormatter{}
    return buf, NewLogrusLogger(logrusLogger)
}

func TestSomething(t *testing.T) {
  // buf can be used to inspect what gets logged if required
  buf, logger := setupLogrusLogger()
  Something(logger)
}

Here, for each package that uses the logger, I have to initialize the logger by defining something like setuplogruslogger. Is there any way to avoid writing this separately for each package I'm writing tests for?

Solution

Export the dependency initialization function and declare it in a non-_test.go file so that other packages can import and reuse it. If you want to separate the testing logic from the normal code of the package, you can move the initialization function into the package that provides the testing utilities.

And, as long as you only import such a test utility package from a _test.go file (and not a normal file), the test utility code will not make it into the compiled binary of the actual program .

Examples of such test utility packages can be found in the standard library:

net/http/httptest

Package httptest provides utilities for HTTP testing.

net/internal/socktest

The socktest package provides utilities for socket testing.

os/exec/internal/fdtest p>

Package fdtest provides test helpers for using file descriptors across execs.

testing/quick

Package Quick implements utility functions to aid in black-box testing.

testing/fstest

Package fstest implements support for file system test implementations and users.

testing/iotest

iotest package implements readers and writers primarily for testing.

The above is the detailed content of How to manage initialization code for test dependencies. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete