如何在 XUnit 中编写端到端测试
How to write an end to end test in XUnit
我是测试和使用 XUnit 为非常大且复杂的项目编写测试的新手。我不想编写单元测试,我只想编写单个测试(一种端到端测试)。
我的项目基本上需要一个文件(包含一些项目,比如 10 个),进行一些处理并根据它们的类型将这些项目推送到不同的列表中。在此之后,一些操作已经完成,如果它们工作正常,那么最后这些项目将从这些列表中删除。
我只想编写一个测试来检查初始文件中的项目数和删除的项目数是否应该相同(检查所有操作是否正常,因为删除是最后一步)。如何通过一次测试做到这一点?
欢迎使用 Whosebug!
您可以像 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 的 Theory if you want to create a single test that uses a range of values to assert different scenarios work. You can also check out FluentAssertions 以获得大量直观且易于阅读的断言,从而使您的测试更加可靠。
测试愉快!
我是测试和使用 XUnit 为非常大且复杂的项目编写测试的新手。我不想编写单元测试,我只想编写单个测试(一种端到端测试)。 我的项目基本上需要一个文件(包含一些项目,比如 10 个),进行一些处理并根据它们的类型将这些项目推送到不同的列表中。在此之后,一些操作已经完成,如果它们工作正常,那么最后这些项目将从这些列表中删除。 我只想编写一个测试来检查初始文件中的项目数和删除的项目数是否应该相同(检查所有操作是否正常,因为删除是最后一步)。如何通过一次测试做到这一点?
欢迎使用 Whosebug!
您可以像 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 的 Theory if you want to create a single test that uses a range of values to assert different scenarios work. You can also check out FluentAssertions 以获得大量直观且易于阅读的断言,从而使您的测试更加可靠。
测试愉快!