# Test::Unit Equivalent of JUnit @AfterClass

**URL:** <https://www.ruby-forum.com/t/test-unit-equivalent-of-junit-afterclass/119587>\
**Category:** Ruby\
**Created:** [November 8, 2007, 6:55am UTC](https://www.ruby-forum.com/t/test-unit-equivalent-of-junit-afterclass/119587 "2007-11-08T06:55:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![patrickjoyce](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9c0ed/32.png) [@patrickjoyce](https://www.ruby-forum.com/u/patrickjoyce)\
**Post date:** [November 8, 2007, 6:55am UTC](https://www.ruby-forum.com/t/test-unit-equivalent-of-junit-afterclass/119587/1 "2007-11-08T06:55:29Z")

</div>

Hey,

Does anyone know a way to do the equivalent of @AfterClass in  
Test::Unit? I need to execute some code once after ALL the test\_\*  
methods in a Test::Unit::TestCase have run (teardown runs after EACH  
method).

The reason I need to do this is that Rails leaves old fixture data in  
the DB after each TestCase is run. This just doesn’t seem right to me,  
as I think each TestCase should start with the same state, but that is  
another issue. The immediate problem is that this can cause tests to  
fail if there are foreign key constraints. For example:

TestCase1 \< Test::Unit::TestCase  
fixtures :testa, :testb  
end

TestCase2 \< Test::Unit:TestCase  
fixtures :testa  
end

If testb has a foreign key that depends upon testa, the second  
TestCase will fail since it will only try to delete from table testa  
and there are records in table testb that depend on testa.

Cliff Moon of PhillyOnRails wrote a plugin  
([http://agilewebdevelopment.com/plugins/foreign\_key\_fixtures](http://agilewebdevelopment.com/plugins/foreign_key_fixtures)) to solve  
this problem, but the svn repository seems to be down.

Another note: I only noticed this when setting up my cruisecontrol.rb  
for my project. The normal “rake test” task sets up the Test DB schema  
from schema.rb which ignores foreign keys. So if you have foreign keys  
in your DB be aware that there is a good chance you are running your  
tests without them.

Any ideas?

- Patrick J.

---

<div class="post-metadata">

**Author:** ![patrickjoyce](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9c0ed/32.png) [@patrickjoyce](https://www.ruby-forum.com/u/patrickjoyce)\
**Post date:** [November 8, 2007, 8:42am UTC](https://www.ruby-forum.com/t/test-unit-equivalent-of-junit-afterclass/119587/2 "2007-11-08T08:42:10Z")

</div>

On Nov 7, 2007, at 21:55 , Patrick J. wrote:

> The reason I need to do this is that Rails leaves old fixture data in  
> the DB after each TestCase is run. This just doesn’t seem right to me,  
> as I think each TestCase should start with the same state, but that is  
> another issue. The immediate problem is that this can cause tests to  
> fail if there are foreign key constraints. For example:

No, it isn’t another issue… it is bug 2404 in the rails bug db.

[http://dev.rubyonrails.org/ticket/2404](http://dev.rubyonrails.org/ticket/2404)

---

<div class="post-metadata">

**Author:** ![patrickjoyce](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9c0ed/32.png) [@patrickjoyce](https://www.ruby-forum.com/u/patrickjoyce)\
**Post date:** [November 8, 2007, 9:22am UTC](https://www.ruby-forum.com/t/test-unit-equivalent-of-junit-afterclass/119587/3 "2007-11-08T09:22:45Z")

</div>

Ryan D. wrote:

> No, it isn’t another issue… it is bug 2404 in the rails bug db.
> 
> [http://dev.rubyonrails.org/ticket/2404](http://dev.rubyonrails.org/ticket/2404)

Thanks, I don’t know how I missed that ticket in all my searching.

How did you end up working around the problem? Did you use the initial  
“nuke\_fixtures” patch? Did you add all the fixtures with foreign keys to  
each test? (that looks like the easiest solution, but just doesn’t seem  
right)

---

<div class="post-metadata">

**Author:** ![patrickjoyce](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9c0ed/32.png) [@patrickjoyce](https://www.ruby-forum.com/u/patrickjoyce)\
**Post date:** [November 8, 2007, 4:53pm UTC](https://www.ruby-forum.com/t/test-unit-equivalent-of-junit-afterclass/119587/4 "2007-11-08T16:53:20Z")

</div>

On Nov 8, 12:39 am, Ryan D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [http://dev.rubyonrails.org/ticket/2404](http://dev.rubyonrails.org/ticket/2404)  
> It’s our favorite bug Ryan!

Yep, going OT here…

That bug alone really made my Rails test suite suffer for an existing  
app. I don’t even try to implement FK’s at the database layer any more  
with Rails as a result.

I wonder if Merb fixed this…

Regards,

Dan
