[关闭]
@jsonquanlee 2021-10-22T15:02:44.000000Z 字数 2687 阅读 267

未分类

Test Automation on Python

PyUnit and Nose2

Pyunit is a unite testing framework on Python. It was added to the Standard Python library in version 2.1, it is compatible with all subsequent versions of the language. Pyunit is a JUNIT implementation on Python, standard Java Unit-Test Framework. That is why the developers who doing Java homework and Python homework will find it very easy to use. Both frameworks are required by its existence framework for testing on Smalltalk from Kent Beck.

Here is an example of a basic unit test:

import unittest

class SimpleWidgetTestCase(unittest.TestCase):
    def setUp(self):
        self.widget = Widget("The widget")

class DefaultWidgetSizeTestCase(SimpleWidgetTestCase):
    def runTest(self):
        assert self.widget.size() == (50,50), 'incorrect default size'

SimpleWidgetTestCase uses the SETUP fixture to create a Widget for testing. DefaultWidGetsIzeTestCase is an heir class SimpleWidgetTestCase, which checks the size of Widget.:
Pyunit is an excellent thing to start setting up testing automation on Python, but this is just a basic set of tools. You will also need tools to automate tests and collect results. Here the game comes Nose.

Nose2 is the next step after Pyunit. It adds support for automatic test detection and plug-ins to perform tests and documentation. The NOSE2 plug-in system adds an additional functionality in the form of decorators, parameterized tests and testing tests. For example, Allmodules find all tests and collects output from them.

NOSE2 also contains such - DSL for writing functional tests.

Pytest

Pytest (https://pytest.org/en/lateest/) - a native testing library on Python, it contains an extended set of Pyunit functions. Compared to the JUNIT architecture modeling, it is definitely written in Python style. It actively uses Python decorators and asserts.

Pytest also supports parametric testing (without NOSE type plug-ins), which simplifies the code to use and its coverage of tests.

If you revet under PyTest, that test that we wrote above, it will look more declarative.

    @pytest.fixture
def widget():
    return Widget("The widget")

def test_widget_size(widget):
    assert widget.size() == (50,50), 'incorrect default size'

Pytest uses test fits for the transfer of the Widget Test Method.

In addition to fixtures, test sets and tests, PyTest has its own support for testing tests. You can select the sets of tests to start, based on the names of methods, pacts or decorators that you add to test code. Still Pytest can perform tests in parallel. When using these functions at the same time, you facilitate the management of large code databases compared to Pyunit.

PyTest simplifies the creation of reports in the form of ordinary text, XML or HTML. You can also add information about code coverage to Pytest reports.

Despite the fact that Pytest can be used independently, you can integrate it with other testing frameworks and test ranners, such as Pyunit and NOSE2. Thanks to this compatibility, PyTest will become an excellent choice for growing projects that need good test coverage. For PyTest, you need Python 3.6 or later versions.

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注