Table of Contents
Question content
Solution
Home Backend Development Golang Go to test coverage, if the statement is not covered

Go to test coverage, if the statement is not covered

Feb 11, 2024 am 10:36 AM

Go to test coverage, if the statement is not covered

php editor Baicao will introduce you to a method of testing coverage today. In the software development process, test coverage is an important indicator, which can help us evaluate the degree of code coverage by test cases. And if we find that there are statements that are not covered, we can solve this problem through some means. In this article, we will explore how to test coverage and how to deal with uncovered statements.

Question content

I have a go function called setupConfig() and I have a Test_setupconfig to test it and it tests fine. But when I tested it for coverage and looked at the HTML report, it showed that my handling of some errors returned by the Viper package were not covered. Why isn't this covered? How should I handle this?

Solution

The coverage report tells you how much code was executed during the test. What you're seeing is that the if statements in these return blocks are not tested, which means you don't have any unit tests designed to fail and return an error. While testing that your code works when given correct input, it's also important to ensure that tests fail correctly and safely when given incorrect input.

However, this is a strange situation because these errors are not in your own package, these errors come from the viper package. At this point, it's important to ask yourself the question "What am I really testing?". If you use the viper package, then it's assumed that the package is thoroughly tested, and by creating your own tests for viper bugs, you're just doubling on those tests without real improvement. For this reason, sometimes we choose to omit testing these branches, because realistically, if the viper package is wrong - and assuming all your inputs are static and hardcoded like shown - then it's not a problem with your code , but it is a problem with the viper library.

If you really want to get 100% coverage and test all decision trees, the only way is to put the viper package behind some abstraction. Most likely it's an interface passed into the function, allowing for multiple implementations whether you're running in production or testing.

Having said that, it is not recommended to hardcode all the values ​​in the function like this. Ideally, you want the configuration structure's values ​​to come from local configuration files, environment variables, command line flags, or a combination thereof. By doing this, you make this setup function accept an interface for retrieving the configuration, making the function easy to test since all you need to do is mock the implementation of the interface in your test. So it will look like this:

config.go

type ConfigController interface {
    GetInput() Config
}

func setupConfig(controller ConfigController) error {
    config := controller.GetInput()
    // your code here
}
Copy after login

config_test.go

type mockConfigController struct {}

func (m *mockConfigController) GetConfig() Config {
    return Config{
        // your config here
    }
}

func Test_setupConfig(t *testing.T) {
    configController := &mockConfigController{}
    err := setupConfig(configController)
    // rest of test here
}
Copy after login

By doing this and providing an accept interface for your setupConfig() function, this means you can provide a function implementation for it when running in production, but also hardcode it when running tests test data to simulate it. It is also often used when interacting with other services, such as databases. Instead of having to start the database and connect to it when you run the test, you can have your code accept an interface that tells it how to interact with the database and simulate it in your tests. This allows you to isolate parts of your code and test only what you want.

The above is the detailed content of Go to test coverage, if the statement is not covered. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the vulnerabilities of Debian OpenSSL What are the vulnerabilities of Debian OpenSSL Apr 02, 2025 am 07:30 AM

OpenSSL, as an open source library widely used in secure communications, provides encryption algorithms, keys and certificate management functions. However, there are some known security vulnerabilities in its historical version, some of which are extremely harmful. This article will focus on common vulnerabilities and response measures for OpenSSL in Debian systems. DebianOpenSSL known vulnerabilities: OpenSSL has experienced several serious vulnerabilities, such as: Heart Bleeding Vulnerability (CVE-2014-0160): This vulnerability affects OpenSSL 1.0.1 to 1.0.1f and 1.0.2 to 1.0.2 beta versions. An attacker can use this vulnerability to unauthorized read sensitive information on the server, including encryption keys, etc.

What is the problem with Queue thread in Go's crawler Colly? What is the problem with Queue thread in Go's crawler Colly? Apr 02, 2025 pm 02:09 PM

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

What libraries are used for floating point number operations in Go? What libraries are used for floating point number operations in Go? Apr 02, 2025 pm 02:06 PM

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

Transforming from front-end to back-end development, is it more promising to learn Java or Golang? Transforming from front-end to back-end development, is it more promising to learn Java or Golang? Apr 02, 2025 am 09:12 AM

Backend learning path: The exploration journey from front-end to back-end As a back-end beginner who transforms from front-end development, you already have the foundation of nodejs,...

PostgreSQL monitoring method under Debian PostgreSQL monitoring method under Debian Apr 02, 2025 am 07:27 AM

This article introduces a variety of methods and tools to monitor PostgreSQL databases under the Debian system, helping you to fully grasp database performance monitoring. 1. Use PostgreSQL to build-in monitoring view PostgreSQL itself provides multiple views for monitoring database activities: pg_stat_activity: displays database activities in real time, including connections, queries, transactions and other information. pg_stat_replication: Monitors replication status, especially suitable for stream replication clusters. pg_stat_database: Provides database statistics, such as database size, transaction commit/rollback times and other key indicators. 2. Use log analysis tool pgBadg

In Go, why does printing strings with Println and string() functions have different effects? In Go, why does printing strings with Println and string() functions have different effects? Apr 02, 2025 pm 02:03 PM

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

How to specify the database associated with the model in Beego ORM? How to specify the database associated with the model in Beego ORM? Apr 02, 2025 pm 03:54 PM

Under the BeegoORM framework, how to specify the database associated with the model? Many Beego projects require multiple databases to be operated simultaneously. When using Beego...

How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? Apr 02, 2025 pm 04:54 PM

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

See all articles