# Best practice: individual or combined tests?

**URL:** <https://www.ruby-forum.com/t/best-practice-individual-or-combined-tests/181566>\
**Category:** Rails\
**Created:** [January 13, 2010, 10:24pm UTC](https://www.ruby-forum.com/t/best-practice-individual-or-combined-tests/181566 "2010-01-13T22:24:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![acreadinglist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@acreadinglist](https://www.ruby-forum.com/u/acreadinglist)\
**Post date:** [January 13, 2010, 10:24pm UTC](https://www.ruby-forum.com/t/best-practice-individual-or-combined-tests/181566/1 "2010-01-13T22:24:06Z")

</div>

Hi folks,

I’m pretty new to TDD. Is there a standard approach on this: Is it  
better to write individual tests, or combine them? A specific example

- if a model Foo validates the presence of both bar and baz, is it  
better to have a test each for bar and for baz, or to combine the  
validations into a combined bar/baz test?

Thanks!

---

<div class="post-metadata">

**Author:** ![acreadinglist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@acreadinglist](https://www.ruby-forum.com/u/acreadinglist)\
**Post date:** [January 13, 2010, 11:51pm UTC](https://www.ruby-forum.com/t/best-practice-individual-or-combined-tests/181566/2 "2010-01-13T23:51:46Z")

</div>

acreadinglist wrote:

> Hi folks,
> 
> I’m pretty new to TDD. Is there a standard approach on this: Is it  
> better to write individual tests, or combine them? A specific example
> 
> - if a model Foo validates the presence of both bar and baz, is it  
> better to have a test each for bar and for baz, or to combine the  
> validations into a combined bar/baz test?

I’d test them individually. In general, you want each test pretty  
atomic.

So for example, take a look at

> <https://github.com/marnen/quorum2/blob/master/spec/models/event_spec.rb>

(the validation specs start at line 217). I’ve got separate specs for  
each validation.

That code could use some DRYing up, though, and I do sometimes do things  
like

```
['one', 'two', 'three'].each do |field|
  it "should require #{field}" do
    @model.send "#{field}=", nil
    @model.should_not be_valid
  end
end

```

but note that this still produces three separate specs. I’d feel wrong  
if it didn’t.

> Thanks!

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![acreadinglist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ccd318/32.png) [@acreadinglist](https://www.ruby-forum.com/u/acreadinglist)\
**Post date:** [January 13, 2010, 11:53pm UTC](https://www.ruby-forum.com/t/best-practice-individual-or-combined-tests/181566/3 "2010-01-13T23:53:38Z")

</div>

Marnen Laibow-Koser wrote:

> acreadinglist wrote:
> 
> > Hi folks,
> > 
> > I’m pretty new to TDD. Is there a standard approach on this: Is it  
> > better to write individual tests, or combine them? A specific example
> > 
> > - if a model Foo validates the presence of both bar and baz, is it  
> > better to have a test each for bar and for baz, or to combine the  
> > validations into a combined bar/baz test?
> 
> I’d test them individually. In general, you want each test pretty  
> atomic.
> 
> So for example, take a look at  
> [quorum2/spec/models/event\_spec.rb at master · marnen/quorum2 · GitHub](http://github.com/marnen/quorum2/blob/master/spec/models/event_spec.rb)  
> (the validation specs start at line 217).

Trust Github to be smart about this! The direct URL for that line (!)  
is

> <https://github.com/marnen/quorum2/blob/master/spec/models/event_spec.rb#L217>

.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
