Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9002765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:36:52+00:00 2026-06-16T00:36:52+00:00

Methods setUp() and tearDown() are invoked before and after each test. But really, is

  • 0

Methods setUp() and tearDown() are invoked before and after each test. But really, is there any real word example about why should I need this?

Inspecting other people tests, I always see something like:

public function setUp()
{
    $this->testsub = new TestSubject();
}

public function tearDown()
{
    unset($this->testsub);
}

public function testSomething()
{
    $this->assertSame('foo', $this->testsub->getFoo());
}

Of course, there is virtually no difference between this way and the “old” local variable way.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:36:53+00:00Added an answer on June 16, 2026 at 12:36 am

    If you do every test method individually, your test code will share a lot of lines that simply create the object to be tested. This shared code can (but not SHOULD) go into the setup method.

    Anything that needs to be done to create the object to be tested then also goes into the setup method, for example creating mock objects that are injected into the constructor of the tested object.

    Nothing of this needs to be teared down because the next call to setup will initialize the class member variables with a new set of objects.

    The only thing that needs teardown is if your test leaves something behind permanently, like files that got created, or database entries. It really isn’t a very good idea to write tests that do such things, but at some point you cannot abstract anymore and have to touch stuff like the harddrive, database or the real network.

    So there is a lot more setup than teardown needed, and I always delete the teardown method if there is no work to be done for this test.

    Regarding mocks, I work like this:

    private $_mockedService;
    private $_object;
    protected function setUp()
    {
        $this->_mockedService = $this->getMock('My_Service_Class');
        $this->_object = new Tested_Class($this->_mockService);
    }
    
    public function testStuff()
    {
        $this->_mockedService->expects($this->any())->method('foo')->will($this->returnValue('bar'));
        $this->assertEquals('barbar', $this->_object->getStuffFromServiceAndDouble());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone explain the use of Python's setUp and tearDown methods while writing test
I want a setup/teardown method to be called before and after a route is
In nUnit, SetUpFixture allowed me to run some code before any tests. Is there
When unit testing with JUnit, there are two similar methods, setUp() and setUpBeforeClass() .
Is there a way to know the number of test methods in a test
Does anyone know the syntax for setUp and tearDown functions/methods for JsTestdriver? On their
Very strange behaviour in my test. public class MyTestclass { @Before void setUp(){ //do
Like most unit testing frameworks, SenTestCase has setUp and tearDown methods you can override
Why does a test fixture have a SetUp method in Google Test? Isn't the
I haven't found a way to test REST methods for my application automatically, while

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.