• pytest中使用assertpy断言


    Pytest中使用assertpy断言

    在我调研assertpy的时候发现了这么一句话:
    Of course, assertpy works best with a python test runner like pytest (our favorite) or Nose.
    那么正好我们也是使用pytest+allure,但是我的断言都是:
    assert 1 = 1
    assert 'x' in 'xyz'
    如果我想对某一段字符串做很多断言呢?

    s = '1234567890'
    assert '1' in s
    assert '0' in s
    

    这样就会写很多assert
    如果我修改成用assertpy

    from assertpy import assert_that
    s='1234567890'
    assert_that(s).contains('1','2','0')  # 表示 1 2  0 都在 s 中
    

    如果报错的话,就是这样子:

    assert_that(s).contains('1','2','0','s')
    # 报错信息
    AssertionError: Expected <1234567890> to contain items <'1', '2', '0', 's'>, but did not contain <s>
    

    我从官网拷贝过来的很多断言方法:

    字符串断言

    assert_that('').is_not_none()
    assert_that('').is_empty()
    assert_that('').is_false()
    assert_that('').is_type_of(str)
    assert_that('').is_instance_of(str)
    
    assert_that('foo').is_length(3)
    assert_that('foo').is_not_empty()
    assert_that('foo').is_true()
    assert_that('foo').is_alpha()
    assert_that('123').is_digit()
    assert_that('foo').is_lower()
    assert_that('FOO').is_upper()
    assert_that('foo').is_iterable()
    assert_that('foo').is_equal_to('foo')
    assert_that('foo').is_not_equal_to('bar')
    assert_that('foo').is_equal_to_ignoring_case('FOO')
    
    assert_that(u'foo').is_unicode()  # on python 2
    assert_that('foo').is_unicode()   # on python 3
    
    assert_that('foo').contains('f')
    assert_that('foo').contains('f','oo')
    assert_that('foo').contains_ignoring_case('F','oO')
    assert_that('foo').does_not_contain('x')
    assert_that('foo').contains_only('f','o')
    assert_that('foo').contains_sequence('o','o')
    
    assert_that('foo').contains_duplicates()
    assert_that('fox').does_not_contain_duplicates()
    
    assert_that('foo').is_in('foo','bar','baz')
    assert_that('foo').is_not_in('boo','bar','baz')
    assert_that('foo').is_subset_of('abcdefghijklmnopqrstuvwxyz')
    
    assert_that('foo').starts_with('f')
    assert_that('foo').ends_with('oo')
    
    assert_that('foo').matches(r'\w')
    assert_that('123-456-7890').matches(r'\d{3}-\d{3}-\d{4}')
    assert_that('foo').does_not_match(r'\d+')
    

    正则匹配

    # partial matches, these all pass
    assert_that('foo').matches(r'\w')
    assert_that('foo').matches(r'oo')
    assert_that('foo').matches(r'\w{2}')
    
    # match the entire string with an anchored regex pattern, passes
    assert_that('foo').matches(r'^\w{3}$')
    
    # fails
    assert_that('foo').matches(r'^\w{2}$')
    
  • 相关阅读:
    VC++MFC对话框程序中给对话添加背景图片
    C++中的引用
    64位Ubuntu 13.04 安装Bochs 2.3.5
    笔记
    ORG 07C00H的意思
    编译三思
    《黑客与画家》笔记
    linux 2.6up的设备和设备驱动模型
    linux嵌入式系统驱动程序的阻塞与异步
    【转】PWM占空比和分辨率
  • 原文地址:https://www.cnblogs.com/tarzen213/p/16425908.html
Copyright © 2020-2023  润新知