如何通过 c# 将附件添加到 ALM OTA 中的测试集?

Posted

技术标签:

【中文标题】如何通过 c# 将附件添加到 ALM OTA 中的测试集?【英文标题】:How to add an attachment to a test set in ALM OTA via c#? 【发布时间】:2015-05-08 12:14:58 【问题描述】:

我运行以下代码,但 ALM 中没有显示任何内容:

AttachmentFactory attachmentFactory = (AttachmentFactory)tsTest.Attachments;
TDAPIOLELib.Attachment attachment = (TDAPIOLELib.Attachment)attachmentFactory.AddItem("test");
attachment.Post();

第二行的 AddItem 方法一直在询问“object ItemData”,但我不知道那到底是什么。惠普的文档很差,实际上没有任何解释。有谁知道如何使用 c# 以编程方式将文件附件添加到 HP ALM 中的测试运行中?

【问题讨论】:

有人吗?布勒?布勒?比勒? 【参考方案1】:

经过大量的痛苦和研究,我找到了答案。我确信还有其他更有效的方法来实现这一点,但由于惠普的文档是地球上最糟糕的,所以这是我能想到的最好的。如果有人有更好的方法,我很想看到它,所以请发布!

我希望这会有所帮助!

try

    if (qcConn.Connected)
    
        string testFolder = @"Root\YourFolder";

        TestSetTreeManager tsTreeMgr = (TestSetTreeManager)qcConn.TestSetTreeManager;
        TestSetFolder tsFolder = (TestSetFolder)tsTreeMgr.get_NodeByPath(testFolder);
        AttachmentFactory attchFactory = (AttachmentFactory)tsFolder.Attachments;
        List tsList = tsFolder.FindTestSets("YourTestNameHere", false, null);

        foreach (TestSet ts in tsList)
        
            TestSetFolder tstFolder = (TestSetFolder)ts.TestSetFolder;
            TSTestFactory tsTestFactory = (TSTestFactory)ts.TSTestFactory;
            List mylist = tsTestFactory.NewList("");
            foreach (TSTest tsTest in mylist)
            
                RunFactory runFactory = (RunFactory)tsTest.RunFactory;
                Run run = (Run)runFactory.AddItem("NameYouWantDisplayedInALMRuns");
                run.CopyDesignSteps();

                //runResult just tells me if overall my test run passes or fails - it's not built in. It was my way of tracking things though the code.
                if(runResult)
                    run.Status = "Failed";
                else
                    run.Status = "Passed";
                run.Post();

                //Code to attach an actual file to the test run.
                AttachmentFactory attachmentFactory = (AttachmentFactory)run.Attachments;
                TDAPIOLELib.Attachment attachment = (TDAPIOLELib.Attachment)attachmentFactory.AddItem(System.DBNull.Value);
                attachment.Description = "Attach via c#";
                attachment.Type = 1;
                attachment.FileName = "C:\\Program Files\\ApplicationName\\demoAttach.txt";
                attachment.Post();

                //Code to attach a URL to the test run
                AttachmentFactory attachmentFactory = (AttachmentFactory)run.Attachments;
                TDAPIOLELib.Attachment attachment = (TDAPIOLELib.Attachment)attachmentFactory.AddItem(System.DBNull.Value);
                //Yes, set the description and FileName to the URL.
                attachment.Description = "http://www.google.com";
                attachment.Type = 2;
                attachment.FileName = "http://www.google.com";
                attachment.Post();

                //If your testset has multiple steps and you want to update 
                //them to pass or fail
                StepFactory rsFactory = (StepFactory)run.StepFactory;
                dynamic rdata_stepList = rsFactory.NewList("");
                var rstepList = (TDAPIOLELib.List)rdata_stepList;
                foreach (dynamic rstep in rstepList)
                
                    if (SomeConditionFailed)
                            rstep.Status = "Failed";
                        else
                            rstep.Status = "Passed";
                        rstep.Post();
                    
                    else
                    
                        rstep.Status = "No Run";
                        rstep.Post();
                    
                
            
        
    

【讨论】:

对于 Java 空类型是:new Variant(Variant.Type.VT_NULL) 希望它可以帮助某人:)【参考方案2】:

我做过类似的事情,但在 Python 中并针对测试步骤,所以即使我没有代码,您也可以复制和粘贴它,这可能会为您指明正确的方向。

而不是调用:

attachmentFactory.AddItem( filename )

调用不带参数的函数(或空参数,因为我从未在 C# 中使用过 OTA API,所以无法判断):

file = attachmentFactory.AddItem()

现在将文件分配给附件项及其其余属性:

file.Filename = "C:\\Users\\myUser\\just\\an\\example\\path" + fileName
file.Description = "File description"
file.Type=1
file.Post()

类型指定它是本地文件,而不是 URL。

【讨论】:

【参考方案3】:

如果有人想知道如何在需求模块上做到这一点,这里是代码:

Req req = Globals.Connection.ReqFactory.Item(*ID*));
VersionControl versionControl = ((IVersionedEntity)req).VC as VersionControl;
versionControl.CheckOut(string.Empty);
AttachmentFactory attFac = req.Attachments;
Attachment att = (Attachment)attFac.AddItem(System.DBNull.Value);
att.Description = "*Your description here";
att.Type = (int)TDAPI_ATTACH_TYPE.TDATT_FILE; //for URL, change here
att.FileName = "*Your path including filename here*";
att.Post();
versionControl.CheckIn("*Your check-in comment here*");

【讨论】:

【参考方案4】:

互联网上没有有价值的信息! 在对 OTA 文档进行了一些挖掘之后,我发现了这一点:

AttachmentFactory attachmentFactory =   (AttachmentFactory)TstTest.Attachments;
            TDAPIOLELib.Attachment attachment = (TDAPIOLELib.Attachment)attachmentFactory.AddItem("demoAttach.txt");
            attachment.Description = "Bug Sample Attachment";
            attachment.Post();
            IExtendedStorage exStrg = attachment.AttachmentStorage;
            exStrg.ClientPath = "E:\\TestData";
            exStrg.Save("demoAttach.txt", true);

实际上,它是 VB 脚本形式,但我设法在 C# 中进行了转换。 OTA参考:

   '-----------------------------------------
'Use Bug.Attachments to
' get the bug attachment factory.
    Set attachFact = bugObj.Attachments
'Add a new extended storage object,an attachment
' named SampleAttachment.txt.
    Set attachObj = attachFact.AddItem("SampleAttachment.txt")
' Modify the attachment description.
    attachObj.Description = "Bug Sample Attachment"
' Update the attachment record in the project database.
    attachObj.Post
' Get the bug attachment extended storage object.
    Set ExStrg = attachObj.AttachmentStorage
'Specify the location of the file to upload.
    ExStrg.ClientPath = "D:\temp\A"
'-----------------------------------------
'Use IExtendedStorage.Save to
' upload the file.
    ExStrg.Save "SampleAttachment.txt", True

【讨论】:

我已经在 c# 中尝试过您的上述代码,但无法将文件附加到测试运行。这对你有用吗?

以上是关于如何通过 c# 将附件添加到 ALM OTA 中的测试集?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 HP ALM C# OTA 中过滤 ID 范围内的缺陷

使用 ALM OTA,我们如何知道 ALM 资源是不是已签出?

使用 python 从 ALM OTA 获取用户字段

使用 python 的 HP ALM 结果附件和状态更新

HP ALM 11 使用 PHP 和 cURL 上传附件

有没有办法使用 vba/macro 一次将多个配置上传到 alm 中的测试用例?