douqie6454 2014-08-29 12:17
浏览 21

从* _test包访问函数

I have a package pkg. The test for package pkg are in _test.go files. However to initialize a test runner I need a a function from other package which I can't import in pkg because of circular dependecy.

My idea is to use pkg_test package.
Is there any way to access test functions (in _test.go files) from pkg in pkg_test?

My project structure:

├── f.go # package pkg ├── f_test.go # package pkg ├── init_test.go # package pkg_test

In other words: I want to access a function from f_test.go in init_test.go or vice versa (access a function from init_test.go in f_test.go. Is there any way to do it?

PS: in f_test.go I can't import pkg_test

  • 写回答

1条回答 默认 最新

  • doujujian0052 2014-08-29 12:31
    关注

    This is generally a good opportunity to:

    • either move the function from pkg_test in pkg
    • or mock whatever the function from pkg_test is doing, in order to not rely on that initialization, through an interface (like mocking database)

    Trying to go around that limitation is trying to work around the dependency issue rather than solving it.

    That being said, if you can import pkg in pkg_test, then yes, you can access a function from f_test.go in init_test.go.

    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错