# Rake test help

**URL:** <https://www.ruby-forum.com/t/rake-test-help/81746>\
**Category:** Ruby\
**Created:** [January 12, 2007, 5:04pm UTC](https://www.ruby-forum.com/t/rake-test-help/81746 "2007-01-12T17:04:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jefers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4bbf92/32.png) [@jefers](https://www.ruby-forum.com/u/jefers)\
**Post date:** [January 12, 2007, 5:04pm UTC](https://www.ruby-forum.com/t/rake-test-help/81746/1 "2007-01-12T17:04:20Z")

</div>

Hi,

I am having difficulty using rake to run tests on a gem I have created.  
I am able to run the tests I have created from the command line  
individually without problem, but when I try to run through rake it  
cannot locate the \*.txt files that I use in the tests.

My folder structure has the tests in a folder under the rake file in  
‘tests’ and the data files the tests are referencing are in  
‘tests/testdata’

The rakefile is set up as

Rake::TestTask.new do |t|  
t.libs \<\< “tests”  
t.test\_files = FileList[‘tests/test\*.rb’]  
t.verbose = true  
end

My test files are referencing \*.txt files as follows:  
def test\_initialize  
o = PAHelper::FilePeeker.new(‘testdata/peeker.txt’, 1)  
puts o.output\_data  
assert\_equal(1, o.output\_data.size)  
end

when I run ‘rake test’ I get the following error:

1. Error:  
test\_initialize(TestFilePeeker):  
Errno::ENOENT: No such file or directory - testdata/peeker.txt  
./tests/…/lib/file\_peeker.rb:8:in `initialize' ./tests/../lib/file_peeker.rb:8:in`initialize’  
tests/test\_file\_peeker.rb:7:in `test\_initialize’

How do I set up my rakefile so that it can find the \*.txt files in the  
testdata folder?

Thanks,

JMG

---

<div class="post-metadata">

**Author:** ![jefers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4bbf92/32.png) [@jefers](https://www.ruby-forum.com/u/jefers)\
**Post date:** [January 19, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/rake-test-help/81746/2 "2007-01-19T16:31:56Z")

</div>

On Sat, Jan 13, 2007 at 01:04:29AM +0900, Jeff Grice wrote:

> I am having difficulty using rake to run tests on a gem I have created.  
> I am able to run the tests I have created from the command line  
> individually without problem, but when I try to run through rake it  
> cannot locate the \*.txt files that I use in the tests.
> 
> My folder structure has the tests in a folder under the rake file in  
> ‘tests’ and the data files the tests are referencing are in  
> ‘tests/testdata’

So you have:  
project\_dir/  
Rakefile  
tests/  
tests/(lots of tests.rb)  
tests/testdata/

And your current directory when you are running ‘rake test’ is  
project\_dir.

> ```
> o = PAHelper::FilePeeker.new('testdata/peeker.txt', 1)
> 
> ```

The bug is here, the current directory when the test is executed is  
‘project\_dir’ but the test assumes it is ‘project\_dir/tests’

> ./tests/…/lib/file\_peeker.rb:8:in `initialize' tests/test_file_peeker.rb:7:in`test\_initialize’
> 
> How do I set up my rakefile so that it can find the \*.txt files in the  
> testdata folder?

If all of your tests are all in test/ directory the easy way would be to  
change your FilePeeker line above to do something like:

```
# make sure we have the directory of the data data files, in this
# case it is relative to the test's directory by being up one and
# then down the 'testdata' directory
def setup
    @data_dir = File.join(File.dirname( __FILE__ ),"..","testdata"))
    ....
end

def test_initialize
    o = PAHelper::FilePeeker.new(File.join(@data_dir,'peeker.txt'))
    puts o.output_data
    assert_equal(1, o.output_data.size)
end

```

Munge as necessary for deep testing directories, etc.

enjoy,

-jeremy

---

<div class="post-metadata">

**Author:** ![jefers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4bbf92/32.png) [@jefers](https://www.ruby-forum.com/u/jefers)\
**Post date:** [September 25, 2007, 10:33pm UTC](https://www.ruby-forum.com/t/rake-test-help/81746/3 "2007-09-25T22:33:12Z")

</div>

Thanks, this worked great.

I added an @data\_dir variable in all of my test setup methods and always  
reference File.join(@data\_dir, “[whatever\_data\_file].txt”) for the data  
file  
and all of my problems are solved!

---

<div class="post-metadata">

**Author:** ![jefers](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/4bbf92/32.png) [@jefers](https://www.ruby-forum.com/u/jefers)\
**Post date:** [September 25, 2007, 10:46pm UTC](https://www.ruby-forum.com/t/rake-test-help/81746/4 "2007-09-25T22:46:50Z")

</div>

On Sat, Jan 13, 2007 at 03:31:56AM +0900, Jeff Grice wrote:

> Thanks, this worked great.
> 
> I added an @data\_dir variable in all of my test setup methods and always  
> reference File.join(@data\_dir, “[whatever\_data\_file].txt”) for the data file  
> and all of my problems are solved!

Glad to help. Keep on having fun 🙂

enjoy,

-jeremy
