Home > Backend Development > Golang > How to import test code from one module to another without exposing it to production code?

How to import test code from one module to another without exposing it to production code?

PHPz
Release: 2024-02-09 21:00:10
forward
627 people have browsed it

How to import test code from one module to another without exposing it to production code?

php editor Yuzi In software development, we often need to test the code. However, we don't want to expose the test code to the production environment to avoid unnecessary confusion. So, how do you import test code from one module to another without exposing it to production code? This is a common problem, but there are solutions. This article will introduce you to an effective method to achieve this goal, making your code more secure and reliable.

Question content

TLDR: I placed a TestLogger struct in logutils/test-logger_test.go and tried to reference it in authentication/login_tests.go. When building tests for the authentication module, the compiler complains that TestLogger is not declared. The only workaround is to rename test-logger_test.go to test-logger.go, but this runs the risk of referencing TestLogger in production code.

When running go tests, golang doesn't seem to import test code (code declared in files with _test.go suffix) from one module to another. Am I doing something wrong or is this the way golang is designed? Is there any solution?

Simple example to illustrate:

I have a "library" module (let's call it il logutils) and an "application" module (let's call it "authentication"). The logutils module has its own tests covering all implementation specific functionality of the loggers (e.g. FileLogger, HttpLogger) and everything is fine.

The logutils module also declares a TestLogger that can be used by "client" modules (such as "authentication") to ensure that they log everything correctly without the need to set up file or http logging. It basically just implements the Logger interface but just logs everything to a buffer that can be verified at the end of the test.

So the "authentication" module can declare its own _test.go file which instantiates TestLogger and it all works like a charm.

What bothers me is that TestLogger needs to be declared in the regular production file (logutils/test-logger.go) and not in the test file (logutils/test-logger_test.go), otherwise the authentication test will complain about TestLogger Not defined. But by doing this, nothing prevents TestLogger from being referenced/used in production code.

What I want is that code in any logutils _test.go file can be imported in any authentication _test.go file (but not in a production .go file).

Any insights/suggestions?

Edit: Fixed typo

Workaround

You can't have this. So simple. (There is no such thing as a "production .go file". Test code is not "non-production".)

The above is the detailed content of How to import test code from one module to another without exposing it to production code?. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template