简体   繁体   English

Ruby测试可以获取Rake执行该文件夹的位置吗?

[英]Can a Ruby test get the location of the folder where Rake executed it from?

Can a Ruby test get the location of the folder where Rake executed it from? Ruby测试可以获取Rake执行该文件夹的位置吗? I want to run Test::Unit unit tests using Rake but my defined "test suites" in Rake need to be able to find locations of libraries relative to the root of my project. 我想使用Rake运行Test::Unit单元测试,但是我在Rake中定义的“测试套件”需要能够找到相对于我的项目根目录的库的位置。

With Maven, I can set a system property like so : 使用Maven,我可以像这样设置系统属性:

<properties>
    <main.basedir>${project.basedir}</main.basedir>
</properties>

And then Java can reference it like so: 然后,Java可以像这样引用它:

String baseDir = System.getProperty("main.basedir");

Can Ruby do something similar? Ruby可以做类似的事情吗? If so, how? 如果是这样,怎么办? Do I need to use a Rake namespace-require + include ? 我是否需要使用Rake名称空间-require + include? Not brewing my own framework: just trying to do the most basic test setup. 没有酝酿自己的框架:只是尝试进行最基本的测试设置。 I do have some lib files I created that my tests want to use. 我确实有一些我要创建的lib文件,这些文件是我的测试要使用的。

This doesn't work because it hard codes the base dir into the class file: 这行不通,因为它会将基本目录硬编码到类文件中:

base_dir = File.expand_path(File.join(File.dirname(__FILE__), ".."))

You can probably do this by injecting it in a test helper and getting the base location relative to the location of the file the test helper is is, but I would like to ask you what are you actually trying to achive? 您可以通过将其注入测试助手中并获取相对于测试助手所在文件位置的基本位置来进行此操作,但是我想问您您实际上想达到什么目标?

normally the tests have access to the lib path and you should be able to just require what you want to use in the tests directly. 通常,测试可以访问lib路径,并且您应该能够直接要求您要在测试中使用的内容。 are you using something like rspec or test::unit? 您使用的是rspec还是test :: unit? are you brewing your own test framework? 您正在酝酿自己的测试框架吗?

so overall the answer is yes you can do it, but you should not have to do it. 因此,总的来说,答案是可以的,但您不必这样做。 can show you how once you clarify what test framework you're using. 可以向您展示如何弄清您正在使用的测试框架。

Edit 编辑

For test unit, this describes almost what you want to do: https://github.com/test-unit/test-unit/blob/master/doc/text/how-to.md 对于测试单元,这几乎描述了您想要执行的操作: https : //github.com/test-unit/test-unit/blob/master/doc/text/how-to.md

The run_test.rb helper is placed in test. 将run_test.rb帮助程序放置在测试中。 You see how the base dir is built by using the path of file (as mentioned above) and how the lib dir is placed on the load path. 您将看到如何使用文件路径(如上所述)构建基本目录,以及如何将lib目录放置在加载路径上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM