单元测试题测试题、单元

2023-09-09 21:54:55 作者:谁的年少不轻狂

我刚开始学习单元测试(使用的FlexUnit)。我仍然不知道如何使用它们。

我创建了一个新的Flex项目,并创建了一个名为 A类。然后,我创建了一个TestCase类,汽车类。

这是弹性生成器给我默认的模板,我把一些痕迹。

 包flexUnitTests
{
    进口flexunit.framework.Assert;

    公共类CarTest
    {
        [之前]
        公共功能设置():无效
        {
            跟踪(测试功能之前');
        }

        [后]
        公共职能的teardown():无效
        {
            跟踪(测试功能后');
        }

        [BeforeClass]
        公共静态函数setUpBeforeClass():无效
        {
            跟踪(测试类前');
        }

        [下课以后]
        公共静态函数tearDownAfterClass():无效
        {
            跟踪(测试类后');
        }

        [测试]
        公共职能testCar():无效
        {
            跟踪(测试功能);

            变种C:汽车=新车(GOL);

            Assert.assertTrue(是车名有效,c.name =勒!);
        }
    }
}
 

我不明白为什么我有这么多的方法来之前或测试功能后运行。为什么不直接使用他们在一个简单的funcion,如:

  [测试]
        公共职能testCar():无效
        {
            跟踪(测试类前');
            跟踪(测试功能之前');

            跟踪(测试功能);

            变种C:汽车=新车(GOL);

            Assert.assertTrue(是车名有效,c.name =勒!);

            跟踪(测试功能后');
            跟踪(测试类后');
        }
 
七上数学第一单元测试题,必考题型

我要添加第二个问题是,这些测试测试每个类individualy,(如instancianting 类时显示)或者我可以通过测试我的整个应用程序一次, instanciating主类?现在的问题是如何将模拟应用程序的整个userflow(用户点击这里,发送请求到服务器那里,等,等)。我应该写一整个流程一一测试方法? :0

解决方案   

我不明白为什么我有这么多的方法来之前或测试功能后运行。

您不要的有无的实现所有这些方法。 FB产生的所有这些存根你,但你还不如让他们出去。

这是说,你可以有一个测试类中一个以上的测试功能。通常你测试类的每个公共职能。你甚至可以在一个函数的多个测试,采用不同组的极端情况的参数。 例如在您的例子中,你只测试了构造,但你可能有一个测试:

car.turnLeft(32)(度) 或看看会发生什么时, car.turnLeft(0) car.turnLeft(190)(你要对地形 car.turnRight 点击这里) 或 car.turnLeft(-12)(负turnLeft?)

的前/方法可以用来编写您想之前执行/在班上每次考试后,一些code,这样你就不必在每个测试功能重复此之后。

  

这些测试individualy测试每个班

这是一个单元测试是不是?

  

我可以通过instanciating主类测试我的整个应用程序一次?

这不是单元测试是:他们只测试类。还有其他类型的测试工具来测试(部分)应用:

在行为测试工具:在你写方案的包含一个典型的执行顺序和断言,这些序列的结果是正确的。我相信黄瓜可以用这种方法测试的Flex应用程序(但不能完全确定) 在UI测试工具:在您的记录场景的用户在应用程序中点击左右。然后,这些情形的可以播放测试是否该应用程序保持运行正常。 FlexMonkey的是Flex应用程序的这样一个解决方案,但似乎所有人都改变了方向与产品。

I just start to study Unit Testing (using FlexUnit). I still have not sure how to use them.

I created a new flex project, and created a class named Car. Then, I created a TestCase class, for Car class.

This is the default template that FlexBuilder gave to me, and I put some traces.

package flexUnitTests
{
    import flexunit.framework.Assert;

    public class CarTest
    {       
        [Before]
        public function setUp():void
        {
            trace('before test function');
        }

        [After]
        public function tearDown():void
        {
            trace('after test function');
        }

        [BeforeClass]
        public static function setUpBeforeClass():void
        {
            trace('before test class');
        }

        [AfterClass]
        public static function tearDownAfterClass():void
        {
            trace('after test class');
        }

        [Test]
        public function testCar():void
        {
            trace('the test function');

            var c:Car = new Car("gol");

            Assert.assertTrue("Is car name valid:", c.name != "gol");
        }
    }
}

I can't understand why I have so many methods to run before or after a test function. Why not just use them within a simple funcion, like:

        [Test]
        public function testCar():void
        {
            trace('before test class');
            trace('before test function');

            trace('the test function');

            var c:Car = new Car("gol");

            Assert.assertTrue("Is car name valid:", c.name != "gol");

            trace('after test function');
            trace('after test class');
        }

A second question I want to add is, these tests test each class individualy, (like shown when instancianting Car class) or a I can test my whole application once by instanciating the main class? The problem is how to simulate the whole userflow of the application (user clicks here, send a request to server there, etc, etc). I should write a whole flow one by one in a test method? :O

解决方案

I can't understand why I have so many methods to run before or after a test function.

You don't have to implement all these methods. FB generated all these stubs for you, but you might as well leave them out.

That said, you can have more than one test function inside a test class. You usually test every public function of a class. You may even have multiple tests per function, using different sets of corner-case arguments. e.g. in your example you only test the constructor, but you might have a test for:

car.turnLeft(32) (in degrees) or see what happens when car.turnLeft(0) or car.turnLeft(190) (you're getting on the terrain of car.turnRight here) or car.turnLeft(-12) (negative turnLeft?)

The before/after methods can be used to write some code that you want executed before/after every test in the class, so that you don't have to repeat this in each test function.

these tests test each class individualy

It is a unit test isn't it?

Can I test my whole application once by instanciating the main class?

That's not what unit tests are for: they only test classes. There are other kinds of testing tools to test (parts of) applications:

behavioural test tools: in which you write scenario's that contain a typical execution sequences and assert that the outcome of such sequences is correct. I believe Cucumber can test Flex apps in this way (but not entirely sure) UI test tools: in which you record scenario's of users clicking around in your application. These scenario's can then be played back to test whether the application keeps running as expected. FlexMonkey is one such solution for Flex apps, but it seems the owners have changed direction with the product.