用于分布式部署的集成测试工具

detoxed的Python项目详细描述


排毒

关于

Build StatuscodecovPyPI versionPyPI - Python VersionPyPI - LicensePyPI - Downloads

Detoxed是一个集成测试工具,可以很容易地在python中运行一套集成测试。很容易集成到cd管道中,目前正用于验证在DevQA阶段中azure iot部署的正确性。在任何允许在部署过程中执行用户定义脚本的cd管道中托管都很容易。

Detoxed可在CD管道中用于:

  • 在部署到目标阶段之后运行集成测试套件
  • 运行正常(启动了吗?)部署到生产阶段后的测试
  • 失败部署的触发器回滚

安装

Detoxed目前支持python版本3.43.53.63.7。它可以通过运行pip install detoxed来安装。

样品

Detoxed附带一个停靠的示例,显示通过和失败的测试。示例显示以下用例:

  • 通过bash调用Detoxed作为cli
  • 作为python库调用Detoxed

要运行示例,请执行以下步骤:

# clone the repo
git clone https://github.com/Microsoft/Detoxed.git
# enter the Detoxed directorycd Detoxed/
# ensure Docker is running
docker --version
# build the project
docker build . -t detoxed
# run the samples
docker run detoxed

基本用法

使用Detoxed的一个常见情况是在本地部署或部署到云配置资源之后执行集成测试。下面是一个简单的集成测试,它将测试应用程序的http端点是否已启动并正在运行:

# assume module name is 'tests.integration.suite'importrequestsfromosimportgetenvfromdetoxed.integimportIntegTestBase,Fail,Pass,TestResultclassSimpleOutboundConnectionTestCase(IntegTestBase):def__init__(self):"""Initialize test with a name of your choice."""super().__init__('Application responds to HTTP GET')# optional method to execute test setup...# def setup(self):#     ...# optional method to execute test teardown...# def teardown(self):#     ...deftest(self)->TestResult:"""Test logic here. Should return Pass() or Fail('<failure message>')"""try:status_code=requests.get(getenv('APP_HTTP_ENDPOINT')).status_codeifstatus_code==200:returnPass()returnFail('status code expected to be 200 but was {}'.format(status_code))exceptExceptionasex:returnFail('failed to execute HTTP GET: {}'.format(ex))# More tests can live in this module or other module.

在本地运行integ测试

下面是如何使用Detoxed

exportAPP_HTTP_ENDPOINT="..."exportTEST_MODULES="tests.integration.suite"# add any number of modules here...
python3 -m detoxed $TEST_MODULES

这将生成以下日志:

importing module: tests.integration.suite
STARTING: 'Application responds to HTTP GET'
✓ PASSED: 'Application responds to HTTP GET'# logs for any additional tests will be here...
✓ PASSED: 'Integration Test Suite'

通过azure dev ops中的部署管道运行integ测试

下面是如何从azure开发人员操作管道调用此集成测试(其他cd技术的步骤类似):

  • 创建链接到发布阶段的变量组。它应包括以下变量:
    • TEST_MODULES:集成测试类所在的模块
    • 任何特定于测试的环境变量。在上面的示例测试中,这将是APP_HTTP_ENDPOINT
  • 创建运行测试线束的脚本步骤:
python3 -m pip install detoxed
# install other modules as needed, perhaps from requirements.txt
python3 -m detoxed $TEST_MODULES

ADO Setup

高级用法

配置部署条件

测试线束将接受指定展开条件的可选参数。此条件定义了一个必须满足的条件,以便测试与超时一起运行,超时指定等待条件通过的时间。将立即满足默认条件,并且不会阻止测试执行。通过使用与此接口匹配的部署条件在python中创建测试工具,可以覆盖此行为:

classDeploymentCondition(ABC):"""Represents a deployment condition that should block the tests from running until met."""@abstractmethoddefis_met(self)->bool:"""True if the condition is met, False otherwise."""@abstractmethoddeftimeout(self)->int:"""Timeout for contition to be met."""

azure物联网边缘部署条件

使用Detoxed的一个常见情况是在部署物联网应用程序之后执行集成测试。集成测试需要在iot部署应用于目标设备之后运行。但是,物联网部署在应用到物联网中心本身之后被视为完成。在将其应用于物联网设备之前,可能需要相当长的时间。处理此情况的内置部署条件随Detoxed一起提供。

fromdetoxed.conditionsimportIoTDeploymentFinishedConditionfromdetoxed.integimportIntegTestRunnerTEST_MODULES=[# your test modules here...]condition=IoTDeploymentFinishedCondition(timeout=300,# seconds to wait for deployment to finishiot_hub='my-iot-hub-qa',# iot hub namedeployment_id=102,# iot deployment IDdevice_query="tags.environment='qa'")# iot device query to enumerate targeted devicestest_harness=IntegTestRunner(TEST_MODULES,condition)passed=test_harness.run()

关于IoTDeploymentFinishedCondition的注释:IoTDeploymentFinishedCondition当前对Azure CLIAzure IoT CLI Extension有硬依赖关系。^必须执行{}才能使IoTDeploymentFinishedCondition正常工作。这可以在azure开发人员操作中通过使用Azure CLI Task运行集成测试,并将Use Global Configuration选项设置为True

待定开发工作

  • 启用并行测试执行

贡献

这个项目欢迎大家提出意见和建议。大多数捐款要求您同意 出资人许可协议(CLA)声明您有权并且实际上有权授予我们 国际扶轮利用你的贡献。有关详细信息,请访问https://cla.microsoft.com

当您提交拉取请求时,cla bot将自动确定您是否需要提供 a cla并适当地装饰pr(例如,标签、注释)。只需按照说明操作 由机器人提供。您只需要使用我们的CLA在所有回购协议中执行一次。

这个项目采用了Microsoft Open Source Code of Conduct。 有关详细信息,请参见Code of Conduct FAQ或 如有任何其他问题或意见,请与opencode@microsoft.com联系。

有关其他指导,请参阅特定于项目的contributing guidelines以及pull request template

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何将cassandra中的行数据转换为与列相关的嵌套json   java如何使用jcr XPath在jcr:content/@jcr:data中搜索?   java在使用openCV进行安卓开发时如何利用手机的广角镜头   java解析扩展了接口,结束了一个潜在的无限循环   位置服务的@Override方法中存在java Android应用程序错误   java本地线程的用途和需求是什么   具有左右子访问的java节点树遍历   java验证JsonWebToken签名   JUL日志处理程序中的java日志记录   嵌入式Java读取给定时间段的串行数据。   java有没有办法从多个URL获取多个图像?   java线程通过等待intent阻止自己发送intent   java Spring MVC解析多部分内容请求   java JPA/Hibernate静态元模型属性未填充NullPointerException   java格式错误的字符(需要引号,得到I)~正在处理   java为什么PrintWriter对象抛出FileNotFoundException?   java Neo4j未正确保存标签   java IE不加载图像