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

How to add an attachment to a test set in ALM OTA via c#?

我运行下面的代码,但在 ALM 中没有任何显示:

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

第二行的 AddItem 方法不断询问 "object ItemData" 但我不知道那到底是什么。 HP 的文档如此糟糕,以至于没有任何解释。有谁知道如何使用 c# 以编程方式将文件附件添加到 HP ALM 中的测试 运行?

我做过类似的事情,但是在 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。

网上没有有价值的信息! 在对 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

经过大量的痛苦和研究,我找到了答案。我敢肯定还有其他方法可以更有效地完成此任务,但是由于惠普的文档是地球上最糟糕的,所以这是我能想到的最好的方法。如果有人有更好的方法,我很乐意看到它,所以请 post 它!

希望对您有所帮助!

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();
                    }
                }
            }
        }
    }
}

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

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*");