dongwei2983 2016-09-24 22:52
浏览 52

针对不同软件包上的所有测试运行单个Golang httptest服务器

My project folder is something like this. It a large API, I separate the files for organization pourposes only.

$ tree src/backend/

src/backend/
├── cats
│   ├── cat_test.go
│   ├── handler.go
│   ├── routes.go
│   └── tools.go
├── cmd
│   ├── populate.go
│   ├── root.go
│   └── runserver.go
├── iot
│   └── device
│      ├── all.go
│      ├── all_test.go
│      ├── router.go
│      ├── types.go
│      └── update.go
├── main.go
├── main_test.go
└── root_test.go

On main_test.go I start the httptest server

func TestMain(m *testing.M) {
    setup()
    exitVal := m.Run()
    teardown()
    os.Exit(exitVal)
}

func setup() {
    flag.StringVar(&util.ServerFlag, "server", "s", "server to set request for")
    flag.Parse()
    viper.SetDefault("testing", true)
    util.BasicStart()
    iot.RunDependencies()
    cats.SyncS3()
    router := cmd.InitRootRouter()
    util.SetTestServer(httptest.NewServer(router))
}

func teardown() {
    log.Println("[TESTS] - Teardown completed")

}

All the tests on root_test.gowork perfctly when I run go test -v ./src/backend/.... But the test at src/backend/cat_test.go fail due to the fact that the teardown method has already been called by the time they run. How can I make the server wait for all test to be run before shutting down?

  • 写回答

1条回答 默认 最新

  • douya5194 2016-09-25 17:58
    关注

    Each package's tests run as a separate binary, so there is no way to share setup and teardown state between packages. If you have a complex setup or teardown that you need to share across packages then you can create a testing package in your project which defines the test scaffold and call it from each test's setup and teardown.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度