繁体   English   中英

如何在 XUnit 中编写端到端测试

[英]How to write an end to end test in XUnit

我是测试和使用 XUnit 为一个非常大和复杂的项目编写测试的新手。 我不想编写单元测试,我只想编写一个测试(一种端到端的测试)。 我的项目基本上需要一个文件(包含一些项目,比如说 10 个),做一些处理并根据它们的类型将这些项目推送到不同的列表中。 现在,在此之后,完成了一些操作,如果它们工作正常,那么最后这些项目将从这些列表中删除。 我只想写一个测试来检查初始文件中的项目数和删除的项目数是否应该相同(检查所有操作是否正常,因为删除是最后一步)。 如何通过一次测试做到这一点?

欢迎使用 StackOverflow!

你可以像 xunit 单元测试一样设置你的测试:

[Fact]
public void Given_When_Then() // describe your test
{
  // Arrange - set up the conditions for your test, perhaps set the contents of the file
  var expectedResults = ...;
  System.IO.File.WriteAllText(@"C:\myTestFile.txt", "line 1\nline 2\nline 3\nline 4\nline 5\nline 6\nline 7\nline 8\nline 9\nline 10");
  var classUnderTest = new ClassUnderTest();

  // Act - perform your action that will impact the data
  var results = classUnderTest.DoProcessing();

  // Assert - prove that your data is in the correct state
  Assert.Equal(results, expectedResults);
  Assert.Equal(System.IO.File.ReadAllLines(@"C:\myTestFile.txt"), expectedResults);  // or perhaps assert on the files
}

如果您想创建一个使用一系列值来断言不同场景工作的单个测试,我个人建议查看 Xunit 的理论 您还可以查看FluentAssertions以获得一堆直观且易于阅读的断言,以使您的测试更加健壮。

测试愉快!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM