# Inheriting test classes

**URL:** <https://www.ruby-forum.com/t/inheriting-test-classes/67063>\
**Category:** Ruby\
**Created:** [August 31, 2006, 11:37am UTC](https://www.ruby-forum.com/t/inheriting-test-classes/67063 "2006-08-31T11:37:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [August 31, 2006, 11:37am UTC](https://www.ruby-forum.com/t/inheriting-test-classes/67063/1 "2006-08-31T11:37:23Z")

</div>

Hi,

All my GUI test classes contain this code within the constructor.

class ST\_MSE\_33  
def initialize

```
start_browser
login

```

…

```
rescue => e
 p "test failed: #{e.message}"
 p e.backtrace
ensure
 log_out
 close_window

```

end  
end

That is, there is always the same start and same end, but a different  
middle.  
Is there any way I could use inheritance to avoid duplication?

Thank You

Aidy

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [August 31, 2006, 12:09pm UTC](https://www.ruby-forum.com/t/inheriting-test-classes/67063/2 "2006-08-31T12:09:25Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

aidy wrote:

> …  
> That is, there is always the same start and same end, but a different  
> middle.  
> Is there any way I could use inheritance to avoid duplication?

I would use the following construct:

class GeneralBehaviour  
def initialize  
start\_browser  
login

```
specific_behaviour

...

```

end

# this is only necessary if GeneralBehaviour can be used

# on its own

def specific\_behaviour  
end  
end

class SpecifB1 \< GeneralBehaviour  
def specific\_behaviour  
# do sth specific 😉  
end  
end

-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.0 (MingW32)

iD8DBQFE9rUj9S2Eui6zfdQRAtN5AKCcjVXYZeUi/Qh1iM3SOay52hLc8gCcDMgc  
4FXA0QPUVqzhFVzr3C6icCY=  
=arYe  
-----END PGP SIGNATURE-----
