$30 off During Our Annual Pro Sale. View Details »

Introduction to Unit Test of Web2p

Introduction to Unit Test of Web2p

Share how to do Unit Test in Python and Web2py Project

Avatar for Villager

Villager

July 27, 2017
Tweet

Other Decks in Programming

Transcript

  1. Agenda What is Unit Test Why we need Unit Test

    Python Unit Test Web2py Highlight Web2py Unit Test
  2. In computer programming, unit testing is a software testing method

    by which individual units of source code, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures, are tested to determine whether they are fit for use. Ideally, each test case is independent from the others. Substitutes such as method stubs, mock objects, fakes, and test harnesses can be used to assist testing a module in isolation. Unit tests are typically written and run by software developers to ensure that code meets its design and behaves as intended. And unit testing can be done manually but is often automated.
  3. Let us validate our component design in isolation, making it

    easier to debug when there's a problem. Let our test functionality which is either not exposed or not easily accessible from the program's external interfaces, for example error handling and functionality which has no UX yet. Provides a cheap way to ensure we haven't broken any major existing functionality across the system when we make cross- component changes. Provides a way for people unfamiliar with our code to verify they haven't broken it when they are working with it. And, for them to see how it works.
  4. Library Required Running unit testing in a Python project, there

    is a python standard library unittest. unittest supports test automation, sharing of setup and shutdown code for tests, aggregation of tests into collections, and independence of the tests from the reporting framework. The unittest module provides classes that make it easy to support these qualities for a set of tests. https:/ /docs.python.org/2/library/unittest.html
  5. To achieve this, unittest supports some important concepts: test case

    A test case is the smallest unit of testing. It checks for a specific response to a particular set of inputs. unittest provides a base class, TestCase, which may be used to create new test cases. test suite A test suite is a collection of test cases, test suites, or both. It is used to aggregate tests that should be executed together.
  6. After unit testing, there will be output information in command

    line. For better visualization and keep test record, there is a open source library nose could generate a test report. With nose, nosetests script could automatically discover and run tests. Within a test directory or package, any python source file matching test{Match} will be examined for test cases. Within a test module, functions and classes whose names match test{Match} and TestCase subclasses with any name will be loaded and executed as tests. Tests may use the assert keyword or raise AssertionError to indicate test failure. TestCase subclasses may do the same or use the various TestCase methods available. http:/ /nose.readthedocs.io/en/latest/
  7. Python Project Unit Test Naming (Required): Because of using nosetest,

    all the test file name should be started with test, testsuite name should be started with Test, testcases name should be started with test. Follow this rule, the nosetest will execute the testcase we defined automatically. Detail steps below: Create a tests folder under root folder Put all the test files in this folder, and all test files name should start with test (ex. test_add_mpoint.py)
  8. Step. 2 Create Class name start with Test and followed

    by unittest.TestCase as param Test Suite name(class name) should be started with Test and the params is required. It is defined in unittest library. With library nose, all test suite start with Test will be executed automatically when unit testing
  9. Step. 3 Initial the test environment There is one initial

    and one end up function called setUp and tearDown. Suggest put params or create fake data that all test case needed in setUp and delete the fake data in tearDown Let params to be global, use self.{params}
  10. Step. 4 Build the test cases All test case function

    name should start with test and use self.{assert method} to build test condition Call function in file which imported previous directly, just like general Python project Assert method reference
  11. Step. 5 Run Python Porject Unit Test Script Move to

    tests folder With nosetests, all test file name start with test, testsuite start with Test and test cases start with test will be executed automatically Could set test report output path add —html-report = {out_path} in script http:/ /nose.readthedocs.io/en/latest/usage.html?highlight=html#options
  12. Web2py is a free, open-source web framework for agile development

    of secure database- driven web applications; it is written in Python and programmable in Python. web2py is a full-stack framework, meaning that it contains all the components you need to build fully functional web applications. Web2py is designed to guide a web developer to follow good software engineering practices, such as using the Model View Controller (MVC pattern). Web2py separates the data representation (the model) from the data presentation (the view) and also from the application logic and workflow (the controller). web2py provides libraries to help the developer design, implement, and test each of these three parts separately, and makes them work together. http:/ /web2py.com/book
  13. Web2py includes a Database Abstraction Layer (DAL) that writes SQL

    dynamically and SQL syntax maps almost one-to-one into DAL syntax, so that you, the developer, don't have to. The DAL knows how to generate SQL transparently for SQLite, MySQL,PostgreSQL, MSSQL, FireBird, Oracle, IBM DB2, Informix and Ingres.
  14. Library Required Running unit testing in a Web2py project, it

    also need the python standard library unittest. And for requesting RESTful APIs, we need to use Web2py library gluon.globals.Request. This library works just like request library in Python, we need to define the request args, request vars, and request method. Like the Python project unit testing, after unit test, there will be output information in command line. For better visualization and keep test record, there is a open source library HTMLTestRunner, HTMLTestRunner could run the testsuite defined with unittest and generate a test report. http:/ /web2py.readthedocs.io/en/latest/globals.html https:/ /github.com/tungwaiyip/HTMLTestRunner/blob/master/HTMLTestRunner.py
  15. Weby2py Unit Test Naming (Option) In Web2py, nosetest is not

    allowable, so no need to follow this naming rules; however, suggest following the rules to make code more readable. Detail steps below: Create a tests folder under root folder Put all the test files in this folder, and all test files name should start with test (ex. test_duo_trip_api.py)
  16. Step. 2 Execute the file which want to do unit

    test In Web2py, unit test scripts don't automatically access to our controllers. This Python method executes your Web2py controller file, bringing all of the function declarations into the local namespace. Passing globals() to the execfile() command lets your controllers see your database
  17. Step. 4 Create Class name start with Test and followed

    by unittest.TestCase as param Test Suite name(class name) should be started with Test and the params is required. It is defined in unittest library.
  18. Step. 5 Initail the test environment There is one initial

    and one end up function called setUp and tearDown. Suggest put params or create fake data that all test case needed in setUp and delete the fake data in tearDown Let params to be global, use self.{params}
  19. Step. 6 Build the test cases All test case function

    name should start with test and use self.{assert method} to build test condition Build Request with args, vars and request_method Call function in controller file imported previous directly to trigger the RESTful API
  20. Step. 7 Set TestSuite, add TestCase into it and set

    HTMLTestRunner to generatereport after unit test Could set test report output path in HTMLTestRunner stream params, then the test report will be generated in the path automatically.
  21. Step. 8 Run Web2py Unit Test Script Move to root

    folder (Web2py folder) -M, --import_models auto import model files; default is False -R PYTHON_FILE, —run=PYTHON_FILE run PYTHON_FILE in web2py environment http:/ /web2py.com/books/default/chapter/29/04/the-core?search=-S#Command-line-options