Avatar ·

How to properly configure test environment (setup) in Go using the testing package?

Hello everyone! I started learning Go and trying to write unit tests for my project. I have a function that works with a database and executes several queries. For testing, I need to prepare data before each test: create tables, insert test records, and clean everything up after the test. I know about TestMain and t.Run functions, but I can't figure out how to properly use them for setup/teardown. In other languages (Java, Python), I'm used to @Before/@After annotations or pytest fixtures. Go doesn't have that, and I feel like I'm copying the same code into every test. Here's an example of my current approach (which I don't like): ```go func TestGetUser(t *testing.T) { db := setupDB() // create DB, tables, insert data defer teardownDB(db) // delete tables ```