pytest 常见组件以及使用(其五)

tmpdir 组件 创建临时目录!
import os
def test_create_file(tmpdir):
    p = tmpdir.mkdir("sub").join("hello.txt")
    p.write("content")
    # tmpdir.listdir() == [local('/tmp/pytest-of-root/pytest-3/test_create_file0/sub')]
    assert p.read() == "content"
    assert len(tmpdir.listdir()) == 1
    assert 0
    
    
@fixture
def non_permission_upload_path(tmpdir):
    file_dir = tmpdir.mkdir('test')
    yield str(file_dir)
    file_dir.remove()


@mark.django_db
def test_upload_data_to_datasource_with_exception(
        mocker, mock_upload_request, create_datasource,
        non_permission_upload_path):
    workspace = mock_upload_request.user.workspace

    os.makedirs(os.path.join(workspace, "ai_datastore/datasource/"))
    mock1 = mocker.patch('os.chown', side_effect=IOError)
    mock2 = mocker.patch('antilles.ai_studio.datasource.datasource_mgt.' +
                         'DatasourceManager.generate_thumbnail')
    image_dir = os.path.join(create_datasource.location,
                             'origin/JPEGImages')
    dmg = DatasourceManager()
    dmg._get_original_data_dir = mocker.MagicMock(return_value=image_dir)
    obj = create_datasource
    total_num = obj.total_num
    with raises(UploadDataException):
        dmg.upload_data_to_datasource(
            mock_upload_request, obj.id)
            # 使用临时目录
    mock_upload_request.data['upload_path'] = non_permission_upload_path
View Code

 

posted @ 2018-11-07 15:25  十七楼的羊  阅读(251)  评论(0编辑  收藏  举报