NUnit的和Web应用程序 [英] Nunit and web applications

查看:220
本文介绍了NUnit的和Web应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始学习单元测试用(NUnit的)。我知道,这种类型的测试是用来测试类,功能和这些功能之间的互动。

I begin studying Unit testing with "(NUnit)". I know that this type of testing is used to test "classes" , "functions" and the "interaction between those functions".

在我来说,我发展的asp.net web应用程序。

In my case I develop "asp.net web applications".


  • 我怎样才能用这个测试来测试我的
    页(因为它被认为是一类
    和使用的方法中),并在其中序列?我有三层:

  • How can i use this testing to test my pages(as it is considered as a class and the methods used in)and in which sequence?, i have three layers:


  1. 接口层(每页的.cs)。

数据访问层(类为每个实体)(DAL)。

Data access layer(class for each entity)(DAL).

数据库层(其中包含连接到数据库,打开连接,关闭连接,...等等)。

Database layer (which contains connection to the database,open connection,close connection,....etc).

商家层(有时使计算或一些独立的逻辑)。

Business layer(sometimes to make calculation or some separate logic).

如何测试,使连接到数据库的方法是什么?

  • How to test the methods that make connection to the database?

    推荐答案

    有单元测试和集成测试。单元测试是测试单个组件/类/方法/功能和它们之间的,但只用一个真实物体(被测-SUT系统)和测试双打相互作用。测试双打可以分为存根和嘲笑。存根提供prepared测试数据SUT。这样,你从环境中隔离SUT。所以,你不必打数据库,Web或WCF服务等方面,你有相同的输入数据的每一次。嘲弄被用来验证SUT正常工作。 SUT调用方法模拟对象,甚至不知道它是不是真正的对象。然后,您通过断言mock对象上验证SUT的作品。你可以手工编写存根和嘲笑或使用许多嘲弄框架之一。其中一个是的http://$c$c.google.com/p/moq/

    There are unit and integration tests. Unit testing is testing single components/classes/methods/functions and interaction between them but with only one real object (system under test-SUT) and test doubles. Test doubles can be divided to stubs and mocks. Stubs provide prepared test data to SUT. That way you isolate SUT from the environment. So You don't have to hit database, web or wcf services and so on and you have same input data every time. Mocks are used to verify that SUT works as expected. SUT calls methods on mock object not even knowing it is not real object. Then You verify that SUT works by asserting on mock object. You can write stubs and mocks by hand or use one of many mocking frameworks. One of which is http://code.google.com/p/moq/

    如果你想测试互动瓦特/数据库的集成测试和一般困难得多。对于集成测试,您必须设置外部资源中众所周知的状态。

    If You want to test interaction w/database that's integration testing and generally is a lot harder. For integration testing You have to setup external resources in well known state.

    让我们来图层:


    1. 您将无法进行单元测试。页是紧耦合到ASP.NET运行时。你应该尽量不要在code多code后面。只需拨打从code一些物体后面,并测试这些对象。你可以看一下MVC设计patters。如果你必须测试你的页面你应该看看 http://watin.org/ 。它自动Internet浏览器,点击网页上的按钮,并验证页面显示预期的结果的。

    1. You won't be able to unit test it. Page is to tightly coupled to ASP.NET runtime. You should try to not have much code in code behind. Just call some objects from your code behind and test those objects. You can look at MVC design patters. If You must test Your page You should look at http://watin.org/. It automates Your internet browser, clicks buttons on page and verifies that page displays expected result's.

    这是集成测试。你把数据在数据库中,然后读出来,并比较结果。试验结束后或测试之前,你必须把测试数据库熟知的状态,这样的测试是可重复的。我的建议是设置数据库测试运行而不是测试运行前后。这样,你就可以检查测试失败后,什么是在数据库中。

    This is integration testing. You put data in database, then read it back and compare results. After test or before test You have to bring test database to well known state so that tests are repeatable. My advice is to setup database before test runs rather then after test runs. That way You will be able to check what's in database after test fails.

    我真的不知道怎么说不同于在一点上没有。 2。

    I don't really know how that differs from that in point no. 2.

    这是单元测试。在测试中创建对象,调用它的方法和验证结果。

    And this is unit testing. Create object in test, call it's methods and verify results.

    如何测试,使数据库的连接在点2 addresed方法。
    如何不浪费时间?这将配备经验。我没有一般建议其他然后不测试不具备任何逻辑属性。

    How to test methods that make connections to the database is addresed in point 2. How to not waste time? That will come with experience. I don't have general advice other then don't test properties that don't have any logic in it.

    有关单元测试这里看看伟大的信息:

    For great info about unit testing look here:

    http://artofunittesting.com/

    http://www.amazon.com/Test-Driven-Development-Kent-Beck/ DP / 0321146530

    http://www.amazon.com/Growing-Object-Oriented-Software-Guided-Tests/dp/0321503627/ref=sr_1_2?ie=UTF8&s=books&qid=1306787051&sr=1-2

    http://www.amazon.com/xUnit-Test-Patterns-Refactoring-$c$c/dp/0131495054/ref=sr_1_1?ie=UTF8&s=books&qid=1306787051&sr=1-1

    编辑:

    SUT,CUT - 被测系统或类。这就是你测试。
    测试双打 - 来自特技双打。他们做的电影场面的危险,使真正的演员不必。同样在这里。测试双打替换测试真实对象,这样就可以在测试中,从环境隔离SUT / CUT。

    SUT, CUT - System or Class under test. That's what You test. Test doubles - comes from stunt doubles. They do dangerous scenes in movies so that real actors don't have to. Same here. Test doubles replace real objects in tests so that You can isolate SUT/CUT in tests from environment.

    让我们来看看这个类

    
    public class NotTestableParty
    {
        public bool ShouldStartPreparing()
        {
            if (DateTime.Now.Date == new DateTime(2011, 12, 31))
            {
                Console.WriteLine("Prepare for party!");
                return true;
            }
            Console.WriteLine("Party is not today");
            return false;
        }
    }
    

    您如何测试这个类做的事情应该在除夕?你必须做的除夕:)

    How will You test that this class does what it should on New Years Eve? You have to do it on New Years Eve :)

    现在看看修改党课
    存根的例子:

    Now look at modified Party class Example of stub:

        public class Party
        {
            private IClock clock;
    
            public Party(IClock clock)
            {
                this.clock = clock;
            }
    
            public bool ShouldStartPreparing()
            {
                if (clock.IsNewYearsEve())
                {
                    Console.WriteLine("Prepare for party!");
                    return true;
                }
                Console.WriteLine("Party is not today");
                return false;
            }
        }
    
        public interface IClock
        {
            bool IsNewYearsEve();
        }
    
        public class AlwaysNewYearsEveClock : IClock
        {
            public bool IsNewYearsEve()
            {
                return true;
            }
        }
    

    现在测试可以假时钟传递到党的阶级

    Now in test You can pass the fake clock to Party class

            var party = new Party(new AlwaysNewYearsEveClock());
            Assert.That(party.ShouldStartPreparing(), Is.True);
    

    现在,你知道,如果你的党类的工作除夕。 AlwaysNewYearsEveClock是a残余部分。

    And now You know if Your Party class works on New Years Eve. AlwaysNewYearsEveClock is a stub.

    现在看一下这个类:

        public class UntestableCalculator
        {
            private Logger log = new Logger();
    
            public decimal Divide(decimal x, decimal y)
            {
                if (y == 0m)
                {
                    log.Log("Don't divide by 0");
                }
    
                return x / y;
            }
        }
    
        public class Logger
        {
            public void Log(string message)
            {
                // .. do some logging
            }
        }

    您将

    如何测试你的类记录的消息。取决于你登录它,你必须检查文件或数据库或其他一些地方。那会不会是单元测试,但集成测试。为了单元测试你做到这一点。

    How will You test that Your class logs message. Depending on where You log it You have to check the file or database or some other place. That wouldn't be unit test but integration test. In order to unit test You do this.

        public class TestableCalculator
        {
            private ILogger log;
            public TestableCalculator(ILogger logger)
            {
                log = logger;
            }
            public decimal Divide(decimal x, decimal y)
            {
                if (y == 0m)
                {
                    log.Log("Don't divide by 0");
                }
                return x / y;
            }
        }
    
        public interface ILogger
        {
            void Log(string message);
        }
        public class FakeLogger : ILogger
        {
            public string LastLoggedMessage;
            public void Log(string message)
            {
                LastLoggedMessage = message;
            }
        }

    和测试您可以

    var logger = new FakeLogger();
            var calculator = new TestableCalculator(logger);
            try
            {
                calculator.Divide(10, 0);
            }
            catch (DivideByZeroException ex)
            {
                Assert.That(logger.LastLoggedMessage, Is.EqualTo("Don't divide by 0"));
            }

    在这里,你断言假冒记录。假记录器是模拟对象。

    Here You assert on fake logger. Fake logger is mock object.

    这篇关于NUnit的和Web应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆