未加星标

Pytest One Solution for Unit, Functional and Acceptance Tests

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二03 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏
python: A tester’s choice

Python is a general purpose, dynamic and flexible language, hence a lot of applications are being developed using Python. From a tester’s perspective, it has readily available modules and libraries which make scripts creation a lot easier. Tests can be written as xUnit-style classes or as functions.It provides full fledge automation testing solution for any sort of project cause and It is capable enough to perform unit, functional, system and BDD testing.

Pytest: Best among all python testing frameworks

Pytest is a python testing framework which provides a single solution for unit, functional and Acceptance testing.

It is popular than the other available frameworks because of its attractive features. Below are some of the features of Pytest:

It offers test design with no boilerplate It does not require separate methods for assertion like assertEquals,assertTrue,assertContains Tests can be parametrized which reduces code duplication Pytest can run tests written in unittest, doctest, autres and nose 150+ external plugins available to support all sorts of functional testing Plugins available like pytest-BDD and pytest-konira for writing tests for Behaviour Driven Testing It works wonder for GUI automation testing, when used along with testing tools like selenium webdriver or splinter

In short pytest is a one stop solution for all sort of python testing be it unit, functional, highly complex functional or acceptance tests (BDD).

Top Most Pytest Features and properties Pytest Fixtures:

Nervous system of pytest

Fixtures are the key concept in pytest which can essentially provide baseline for test creation and execution. To declare any method as fixture just put the annotation for the method “@pytest.fixture” and put them in “Conftest.py” Fixture example:

@pytest.fixture

defopen_browser():

driver = webdriver.Firefox()

driver.get( “http://www.python.org” )

assert “Python” in driver.title

The above designed fixture will be available for the whole project provided this should specified in project directory conftest.py file.

Conftest.py file contains all configuration settings, all defined fixtures, hooks implementations and it is applicable to the directory level. They get loaded by default whenever tool is invoked. Some key points on fixtures: Fixtures have names and can be called from anywhere in the project, modules, classes and tests Fixtures can return or not return any value or just execute the specified steps in it. Fixtures can be passed as function arguments and in that case the returning value of fixture will be available in the mentioned method. Fixture can be specified in directory level conftest.py file. Can be called from any method and it will execute the steps specified in it. A fixture can take multiple fixtures, and each fixture triggers other fixture, thus serves a modular approach. Fixtures can be scoped as per the need. This is a good practice keeping in mind time-expensiveness. Scope can be “session”, “module”, “class” and “function”. Request object:

Introspect agent

This is one of the useful features to search or introspect the “requesting item”. It can introspect from test function, class, module, or session. Items specified in config file or returning by some other fixtures can be used efficiently by the called fixture by “getattr” function. Just check the below mentioned fixture: Example:

@pytest.fixture

defdriver(request, browser_type):

“””return a webdriver instance for testing

try :

_driver = getattr(webdriver, browser_type)()

exceptAttributeError as error:

logger.info( “Browser type %s unavailable!” , browser_type)

raiseAttributeError

return_driver

Finalization code:

Setting up of teardown

Fixture can be used for teardown as well. This can be achieved by using “yield” keyword. Just put the fixture with the annotation “@pytest.yield_fixture“ and put the steps after “yield” keyword in the fixture. And whenever fixture will go out of scope the steps after the “yield” keyword will serves as teardown process. Have a look at the below modified steps of the “driver” fixture. Example:

@pytest.yield_fixture

defdriver(request, browser_type):

“””return a webdriver instance for testing

try :

_driver = getattr(webdriver, browser_type)()

exceptAttributeError as error:

logger.info( “Browser type %s unavailable!” , browser_type)

raiseAttributeError

yield_driver

logger.info( ‘Finishing test”)

logger.info( ‘*************************’ )

_driver.quit()

In the above mentioned fixture whenever “browser_type” is not available the driver instance will be quit.

Fixture parametrization:

Enhances reusability

Fixtures can be parametrized and can be executed multiple times with different set of data, the same way as a normal function is executed. Usefixtures:

Call for fixture from anywhere

One way by which fixtures can be made available anywhere in the project is by calling them by using the decorator “@pytest.mark.usefixtures(“fixture_1”, “fixture_2”)”. Autouse fixture:

Mark the fixture for all

AF are the fixtures methods which get invoked without “usefixtures” decorator or “funcgars”.

Any fixture can be registered for autouse. Just need to put keyword autouse with “True” flag. “pytest.fixture(scope=”module”,autouse=True)”. Fixture will run for a class or test or module as mentioned in the scope. If they are defined in conftest then they will be invoked by the all tests present below the directory. These fixtures are particularly useful to set applicable global settings for the test session.

Auto Test Discovery in Pytest:

Ease of execution

One of the very useful features is auto test discovery in pytest. Its means it detects all the tests once execution command is invoked, user only need to specify the test modules and test with a prefix “test_*.” while designing . Command line arguments can be specified with tests names, directories, node ids and file names. In case of absence of any command line arguments then collection with start from specified ‘testpaths’ (provided they need to be configured). This feature help in running all the tests, multiple tests in groups, single test, and tests belong to specific directories. Alternatively test can be organized in specific folder structure as per the modules and thus can be executed as per the need.

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

主题: PythonFirefox
分页:12
转载请注明
本文标题:Pytest One Solution for Unit, Functional and Acceptance Tests
本站链接:http://www.codesec.net/view/524216.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(86)