What are fixtures in programming?

前端 未结 6 638
闹比i
闹比i 2020-12-22 15:56

I have heard of this term many times (in the context of programming) but couldn\'t find any explanation of what it meant. Any good articles or explanations?

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-22 16:21

    I think PHP-unit tests have very good explaining of this:

    One of the most time-consuming parts of writing tests is writing the code to set the world up in a known state and then return it to its original state when the test is complete. This known state is called the fixture of the test.

    Also Yii documents described fixtures test in a good shape:

    Automated tests need to be executed many times. To ensure the testing process is repeatable, we would like to run the tests in some known state called fixture. For example, to test the post creation feature in a blog application, each time when we run the tests, the tables storing relevant data about posts (e.g. the Post table, the Comment table) should be restored to some fixed state.

    Here the simple example of fixtures test

    stack = [];
        }
    
        protected function tearDown()
        {
            $this->stack = [];
        }
    
        public function testEmpty()
        {
            $this->assertTrue(empty($this->stack));
        }
    
        public function testPush()
        {
            array_push($this->stack, 'foo');
            $this->assertEquals('foo', $this->stack[count($this->stack)-1]);
            $this->assertFalse(empty($this->stack));
        }
    
        public function testPop()
        {
            array_push($this->stack, 'foo');
            $this->assertEquals('foo', array_pop($this->stack));
            $this->assertTrue(empty($this->stack));
        }
    }
    ?>
    

    This PHP unit test has functions with names setUp and tearDown that before running your test you setup your data and on finished you can restore them to the initial state.

提交回复
热议问题