developer tip

F # 개발 및 단위 테스트?

copycodes 2020. 8. 13. 23:34
반응형

F # 개발 및 단위 테스트?


첫 번째 기능 언어 인 F #으로 막 시작했습니다. 저는 C #과 유사하게 작업 해 왔으며 F #이 코드 작성 방법을 다시 생각하게하는 방법을 많이 즐깁니다. 내가 조금 혼란스러워하는 한 가지 측면은 코드 작성 과정의 변화입니다. 저는 C #에서 수년 동안 TDD를 사용해 왔으며 현재 위치를 알기 위해 단위 테스트를 해주셔서 감사합니다.

지금까지 F #을 사용한 저의 프로세스는 몇 가지 함수를 작성하고, "합리적으로"작동하는지 "합리적으로"확인 될 때까지 대화 형 콘솔로 재생하고, 조정 및 결합하는 것이 었습니다. 이것은 오일러 프로젝트와 같은 소규모 문제에서 잘 작동하지만 그렇게 큰 것을 만드는 것을 상상할 수 없습니다.

사람들은 단위 테스트에 어떻게 접근하고 F # 프로그램을위한 테스트 제품군을 구축합니까? TDD에 상응하는 것이 있습니까? 어떤 조언이나 생각이라도 감사합니다.


테스트 기반 개발자는 F #과 같은 기능적 언어를 사용하는 편이 좋습니다. 결정적으로 반복 가능한 결과를 제공하는 작은 함수가 단위 테스트에 완벽하게 적합합니다. F # 언어에는 테스트 작성을 용이하게하는 기능도 있습니다. 예를 들어, Object Expressions . 인터페이스 유형을 입력으로 취하는 함수에 대한 가짜를 매우 쉽게 작성할 수 있습니다.

F #은 최고 수준의 개체 지향 언어이며 C #에서 TDD를 수행 할 때 사용하는 것과 동일한 도구와 트릭을 사용할 수 있습니다. F #으로 작성되었거나 특별히 작성된 몇 가지 테스트 도구도 있습니다.

Matthew Podwysocki는 기능적 언어의 단위 테스트에 대한 훌륭한 시리즈썼습니다 . 밥 삼촌도 여기에 생각을 자극하는 기사를 썼다 .


나는 NUnit을 사용하고 있으며 읽기 어렵거나 쓰기가 번거롭지 않습니다.

open NUnit.Framework

[<TestFixture>]
type myFixture() = class

    [<Test>]
    member self.myTest() =
       //test code

end

내 코드는 F #과 다른 .Net 언어가 혼합되어 있기 때문에 단위 테스트를 기본적으로 동일한 방식으로 F #과 C #에서 유사한 구문으로 작성한다는 사실이 마음에 듭니다.


한 번 봐 가지고 FsCheck , F #의 자동 테스트 도구를 기본적으로 하스켈의 QuickCheck의 포트입니다. 이를 통해 함수 나 메서드가 충족해야하는 속성 형식으로 프로그램의 사양을 제공 할 수 있으며, FsCheck는 속성이 무작위로 생성 된 많은 경우에서 보유하는지 테스트합니다.

FsCheck CodePlex 페이지

FsCheck 작성자 페이지


dglaubman이 제안했듯이 NUnit을 사용할 수 있습니다. xUnit.net 도 이에 대한 지원을 제공하며 TestDriven.net 과 잘 작동합니다 . 코드는 NUnit 테스트와 비슷하지만 포함 된 유형으로 테스트를 래핑 할 필요가 없습니다.

#light

// Supply a module name here not a combination of module and namespace, otherwise
// F# cannot resolve individual tests nfrom the UI.
module NBody.DomainModel.FSharp.Tests

open System
open Xunit

open Internal

[<Fact>]
let CreateOctantBoundaryReordersMinMax() =
    let Max = VectorFloat(1.0, 1.0, 1.0)
    let Min = VectorFloat(-1.0, -1.0, -1.0)

    let result = OctantBoundary.create Min Max

    Assert.Equal(Min, result.Min)     
    Assert.Equal(Max, result.Max) 

제 자신이 많이 궁금해했던 매우 흥미로운 질문이라고 생각합니다. 지금까지의 내 생각은 생각 일 뿐이므로 그대로 가져 가십시오.

자동화 된 테스트 스위트의 안전망은 놓치기에는 너무 가치가 있다고 생각하지만 대화 형 콘솔이 매력적일 수 있으므로 항상 그래 왔듯이 단위 테스트를 계속 작성할 계획입니다.

One of the main strengths of .NET is the cross-language capabilities. I know I'm going to be writing F# production code soon, but my plan is to write unit tests in C# to ease my way into what is for me a new language. In this way, I also get to test that what I write in F# will be compatible with C# (and other .NET languages).

With this approach, I understand that there are certain features of F# that I can only use internally in my F# code, but not expose as part of my public API, but I will accept that, just as I accept today that there are certain things C# allows me to express (like uint) that aren't CLS compliant, and so I refrain from using them.


You could have a look at FSUnit - though I haven't used it yet, it might worth a try. Certainly better than using for example (native) NUnit in F#.


Despite being a bit late to the party, I'd like to welcome Mathias to F# (better late than never ;)) and chime in that you might like Expecto

Expecto has some features you might like:

  • F# syntax throughout, tests as values; write plain F# to generate tests
  • Use the built-in Expect module, or an external lib like Unquote for assertions
  • Parallel tests by default
  • Test your Hopac code or your Async code; Expecto is async throughout
  • Pluggable logging and metrics via Logary Facade; easily write adapters for build systems, or use the timing mechanism for building an InfluxDB+Grafana dashboard of your tests' execution times
  • Built in support for BenchmarkDotNet
  • Build in support for FsCheck; makes it easy to build tests with generated/random data or building invariant-models of your object's/actor's state space

--

open Expecto

let tests =
  test "A simple test" {
    let subject = "Hello World"
    Expect.equal subject "Hello World" "The strings should equal"
  }

[<EntryPoint>]
let main args =
  runTestsWithArgs defaultConfig args tests

https://github.com/haf/expecto/

참고URL : https://stackoverflow.com/questions/1989487/f-development-and-unit-testing

반응형