EzDevInfo.com

Testify

A more pythonic testing framework.

Go: Run test from multiple package with DB initialization

I have a GO project with this project structure (multiple couples of this kind of files in each package).

- api
    - userHandler.go
    - userHandler_test.go
- database
    - user.go
    - user_test.go

Inside user.go I have the User struct and the functions to Create/Get/Update a User (I'm using GORM but this is not the issue). In the user_test.go.

I'd like to have the DB cleaned (with all the data removed or in a certain state) for each different file, so I've tried to create 1 suite (using Testify) for each file, then use the SetupSuite function but the behaviour seems not deterministic, and probably I'm doing something wrong.

So my questions are:

  • Which is the best way to have a DB connection shared? Using a global variable is the best option?
  • Which is the best way to create the tables in the DB once and then init the DB with custom data before each file_test.go is run?

Right now I'm also having a strange bug: running

go test path/package1
go test path/package2

Everything works fine, but if I run (for testing all the packages)

cd path && go test ./...

I have errors that seems not to be deterministic, that's why I'm guessing that the DB connection is not handled properly


Source: (StackOverflow)

How to test Golang channels / go-routines

I have a type that contains a byte of data, and takes a channel to post new data there. Other code can read the last written byte of data using a Read function.

Edit: for actual, runnable code, see https://github.com/ariejan/i6502/pull/3 especially files acia6551.go and acia6551_test.go. Tests results can be viewed here: https://travis-ci.org/ariejan/i6502/jobs/32862705

I have the following:

// Emulates a serial interface chip of some kind.
type Unit struct {
  // Channel used for others to use, bytes written here will be placed in rxChar
  Rx chan byte

  // Internal store of the last byte written.
  rxChar byte // Internal storage
}

// Used internally to read data store in rxChar
func (u *Unit) Read() byte {
  return u.rxChar
}

// Create new Unit and go-routing to listen for Rx bytes
func NewUnit(rx chan byte) *Unit {
  unit := &Unit{Rx: rx}

  go func() {
    for {
      select {
      case data := <-unit.Rx:
        unit.rxData = data
        fmt.Printf("Posted 0x%02X\n", data)
      }
    }
  }()

  return unit
}

My test looks like this:

func TestUnitRx(t *testing.T) {
  rx := make(chan byte)
  u := NewUnit(rx)

  // Post a byte to the Rx channel
  // This prints "Posted 0x42", as you'd expect
  rx <- 0x42

  // Using testing
  // Should read last byte, 0x42 but fails.
  fmt.Println("Reading value...")
  assert.Equal(t, 0x42, u.Read()) 
}

At first I figured the "Reading value" happened before the go-routing got around to writing the data. But the "Posted" message is always printed before "Reading".

So, two questions remain:

  • Is this the best way to handle an incoming stream of bytes (at 9600 baud ;-))
  • If this is the right way, how can I properly test it or what is wrong with my code?

Source: (StackOverflow)

Advertisements

testify (Golang) seemingly running test suites concurrrently?

Basically I created a new test file in a particular package with some barebone test structure - no actual tests...just an empty struct type that embeds suite.Suite, and a function that takes in a *testing.T object and calls suite.Run() on said struct. This immediately caused all our other tests to start failing indeterministically.

The nature of the failures were associated with database unique key integrity violations on inserts and deletes into a single Postgres DB. This is leading me to believe that the tests were being run concurrently without calling our setup methods to prepare the environment properly between tests.

Needless to say, the moment I move this test file to another package, everything magically works!

Has anyone else run into this problem before and can possibly provide some insights?

enter image description here


Source: (StackOverflow)

Separating unit tests and integration tests in GoLang (testify)

Is there an established best practice for separating unit tests and integration tests in GoLang (testify)? I have a mix of unit tests (which do not rely on any external resources and thus run really fast) and integration tests (which do rely on any external resources and thus run slower). So, I want to be able to control whether or not to include the integration tests when I say go test.

The most straight-forward technique would seem to be to define a -integrate flag in main:

var runIntegrationTests = flag.Bool("integration", false
    , "Run the integration tests (in addition to the unit tests)")

And then to add an if-statement to the top of every integration test:

if !*runIntegrationTests {
    this.T().Skip("To run this test, use: go test -integration")
}

Is this the best I can do? I searched the testify documentation to see if there is perhaps a naming convention or something that accomplishes this for me, but didn't find anything. Am I missing something?


Source: (StackOverflow)