使用Visual Studio进行Fortran单元测试

3

各位好,

我试图寻找有关Fortran和Visual Studio中的单元测试的任何现有文档,但没有找到。 你是否有进行Fortran单元测试的经验,可以为该主题推荐一些好的教程或书籍?这些工具需要集成到Visual Studio 19环境中,并且我希望使用内置的Test-Explorer。(我使用英特尔编译器,但是这对于测试不应该有影响,对吧?)

最好的问候,

2个回答

1

这些工具需要集成到Visual Studio 19环境中,我很想使用内置的Test-Explorer。 - CTZStef

0

Fortran单元测试库FUT

dongli创建了这个易于应用的Fortran单元测试库:

https://github.com/dongli/fortran-unit-test

以下示例展示了此库的用法:
program good_test

  use unit_test

  implicit none

  type(test_suite_type)  specific_suite

  ! example with default suite
  call test_case_init()

  call test_case_create('Test 1')

  ! By sending macros __FILE__ and __LINE__, report will print the file and line number where assertion fails.
  call assert_approximate(1.0, 2.0, __FILE__, __LINE__) ! line 14

  call test_suite_report()
  call test_case_final()

  ! example with specific suite
  specific_suite%name = 'my specific test suite'
  call test_case_create('Specific Test 1', specific_suite)
  ! suite = SUITE need in this case (cause optional argument eps, file_name, line_number is missing)
  call assert_approximate(1.0, 2.0, suite=specific_suite)

  call test_case_create('Specific Test 2', specific_suite)
  ! suite = SUITE need in this case (cause optional argument eps is missing)
  call assert_equal(1.0, 2.0, __FILE__, __LINE__,  suite=specific_suite)

  call test_case_create('Specific Test 3', specific_suite)
  call assert_approximate(1.0, 2.0, __FILE__, __LINE__, 1E-0, specific_suite)

  ! report a test_case
  call test_case_report('Specific Test 2', specific_suite)

  ! report the complete suite
  call test_suite_report(specific_suite)
  call test_case_final(specific_suite)

end program good_test

输出的样子如下:

///////////////////// Report of Suite: Default test suite ///////////////////////

 +-> Details:
 |   |
 |   +-> Test 1: 1 of 1 assertions succeed.
 |   |
 |
 +-> Summary:
 |   +-> Default test suite: 1 of 1 assertions succeed.

////////////////////////////////////////////////////////////////////////////////


//////// Report of Suite: my specific test suite, Case: Specific Test 2 /////////

 +-> Specific Test 2: 0 of 1 assertions succeed.
 |   |
 |   +-> Assertion #1 failed with reason: x ( 1.000) == y ( 2.000)
 |   +-> Check line: test_assert.F90:29

/////////////////// Report of Suite: my specific test suite /////////////////////

 +-> Details:
 |   |
 |   +-> Specific Test 1: 1 of 1 assertions succeed.
 |   |
 |   +-> Specific Test 2: 0 of 1 assertions succeed.
 |   |   |
 |   |   +-> Assertion #1 failed with reason: x ( 1.000) == y ( 2.000)
 |   |   +-> Check line: test_assert.F90:29
 |   |
 |   +-> Specific Test 3: 0 of 1 assertions succeed.
 |   |   |
 |   |   +-> Assertion #1 failed with reason: x ( 1.000) =~ y ( 2.000)
 |   |   +-> Check line: test_assert.F90:32
 |   |
 |
 +-> Summary:
 |   +-> my specific test suite: 1 of 3 assertions succeed.

////////////////////////////////////////////////////////////////////////////////

1
这些工具需要集成到Visual Studio 19环境中,我很想使用内置的Test-Explorer。 - CTZStef
@CTZStef:由于该项目已经在GitHub上可用,您可以尝试联系开发者dongli,看他是否有兴趣为此编写Visual Studio扩展,或者您也可以自己尝试。如果您觉得我的回答有帮助,请随意点赞。谢谢。 - CKE
事实上,我并没有发现你的答案有任何用处,因为问题明确提到了 Visual Studio。 - CTZStef
1
感谢您的澄清。老实说,我正在使用Intel Fortran和Visual Studio 2019开发软件,并使用这个单元测试套件。它完全符合我的需求(控制台应用程序,数字计算)。当然,在开始时需要进行一些配置,但这是目前我在Visual Studio和Fortran软件开发方面找到的最好的工具。 - CKE
哦,我明白了。那么我想说的是——这只是一个没有花太多时间研究的人的意见——缺少的是我们如何在VS2019中集成这个单元测试框架。我也在使用Intel Fortran,我非常想知道你是如何做到的。 - CTZStef

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接