我一直在为程序iText寻找一些单元测试,但没有运气。 有没有人知道任何这样的测试? 另外,有谁知道开发人员是否在iText上使用任何自动测试工具,例如Jenkins?
我在.NET Core 6的iText7中。我知道这个问题很老了,但希望这对某人有所帮助。
以下是我们如何测试段落文本字段的方法,如下所示。
public class PdfHelper
{
internal static Table GetTableCustInfo(string fName, string lName)
{
// This method would probably take params and be called from a public "build" method in this class
var custInfo = new Table(1);
custInfo.SetHorizontalAlignment(HorizontalAlignment.LEFT);
custInfo.SetTextAlignment(TextAlignment.LEFT);
custInfo.AddCell(new Cell().Add(new Paragraph(fName)));
custInfo.AddCell(new Cell().Add(new Paragraph(Lname)));
return custInfo;
}
}
单元测试:
[TestMethod]
public void GetTableCustInfo_lNameProvided_lNameMaps()
{
Table actual = PdfHelper.GetTableCustInfo("Popeye", "TheSailor");
var actualText = GetTextFromCell(actual, 1, 0);
Assert.AreEqual("TheSailor", actualText);
}
private static string GetTextFromCell(Table table, int row, int column)
{
Cell targetCell = table.GetCell(row, column);
IElement cellChild = targetCell.GetChildren()?.FirstOrDefault();
Assert.IsNotNull(cellChild, "Cell child should exist");
Paragraph childParagraph = (Paragraph)cellChild;
Assert.IsNotNull(childParagraph, "Child's paragraph should exist.");
IElement childOfParagraph = childParagraph.GetChildren()?.FirstOrDefault();
Assert.IsNotNull(childOfParagraph, "The paragraph has no children.");
Text paragraphTextElement = (Text)childOfParagraph;
Assert.IsNotNull(paragraphTextElement, "Child of paragraph is missing the text element.");
var textValue = paragraphTextElement.GetText();
// Finally tracked it down!
return textValue;
}