Skip to content

Commit

Permalink
Fake.DotNet.Testing.SpecFlow: Added tests and optimized API
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmonty committed Oct 11, 2018
1 parent 0a4289d commit b9e4a32
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 37 deletions.
14 changes: 7 additions & 7 deletions help/markdown/fake-dotnet-testing-specflow.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ open Fake.DotNet.Testing
let specsProject = "IntegrationTests.csproj"
Target.create "Regenerate Test Classes" (fun _ ->
SpecFlowNext.run (fun p ->
{ p with ProjectFile = specsProject })
specsProject |> SpecFlowNext.run id
)
Target.create "Create StepDefinition Report" (fun _ ->
SpecFlowNext.run (fun p ->
{ p with SubCommand = StepDefinitionReport
ProjectFile = specsProject
BinFolder = Some "bin/Debug"
OutputFile = Some "StepDefinitionReport.html" })
specsProject
|> SpecFlowNext.run (fun p ->
{ p with
SubCommand = StepDefinitionReport
BinFolder = Some "bin/Debug"
OutputFile = Some "StepDefinitionReport.html" })
)
Target.create "Default" Target.DoNothing
Expand Down
25 changes: 14 additions & 11 deletions src/app/Fake.DotNet.Testing.SpecFlow/SpecFlowNext.fs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ type SubCommand =
/// SpecFlow execution parameter type.
type SpecFlowParams = {
SubCommand: SubCommand
ProjectFile: string
ToolName: string
ToolPath: string
WorkingDir: string
Expand All @@ -45,7 +44,6 @@ let private currentDirectory = Directory.GetCurrentDirectory ()
/// SpecFlow default execution parameters.
let private SpecFlowDefaults = {
SubCommand = GenerateAll
ProjectFile = null
ToolName = toolname
ToolPath = Tools.findToolFolderInSubPath toolname (currentDirectory </> "tools" </> "SpecFlow")
WorkingDir = null
Expand All @@ -59,7 +57,12 @@ let private SpecFlowDefaults = {
XsltFile = None
}

let internal createProcess setParams =
let internal createProcess setParams projectFile =
if projectFile |> String.isNullOrWhiteSpace
then
Trace.traceError "SpecFlow needs a non empty project file!"
failwithf "SpecFlow needs a non empty project file!"

let parameters = setParams SpecFlowDefaults
let tool = parameters.ToolPath </> parameters.ToolName

Expand All @@ -74,9 +77,8 @@ let internal createProcess setParams =
[
yield parameters.SubCommand |> string

if not (isNull parameters.ProjectFile) then
yield "--ProjectFile"
yield parameters.ProjectFile
yield "--ProjectFile"
yield projectFile

yield! parameters.BinFolder
|> yieldIfSome "binFolder"
Expand All @@ -95,28 +97,29 @@ let internal createProcess setParams =
yield! parameters.FeatureLanguage
|> yieldIfSome "FeatureLanguage"

if parameters.Verbose then yield "verbose"
if parameters.ForceRegeneration then yield "force"
if parameters.Verbose then yield "--verbose"
if parameters.ForceRegeneration then yield "--force"

yield! parameters.XsltFile
|> yieldIfSome "XsltFile"
]
|> Arguments.OfArgs
//|> Args.toWindowsCommandLine

Trace.trace (tool + " " + args.ToStartInfo)

parameters,
CreateProcess.fromCommand (RawCommand(tool, args))
|> CreateProcess.withFramework
|> CreateProcess.withWorkingDirectory parameters.WorkingDir
|> CreateProcess.ensureExitCode

// Runs SpecFlow on a project.
/// ## Parameters
///
/// - `setParams` - Function used to manipulate the default SpecFlow parameter value.
let run setParams =
let parameters, cp = createProcess setParams
/// - `projectFile` - The required project file.
let run setParams projectFile =
let parameters, cp = projectFile |> createProcess setParams
use __ = Trace.traceTask "SpecFlow " (parameters.SubCommand |> string)
cp
|> Proc.run
Expand Down
73 changes: 54 additions & 19 deletions src/test/Fake.Core.UnitTests/Fake.DotNet.Testing.SpecFlow.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,65 @@ module Fake.DotNet.Testing.SpecFlowTests

open System.IO
open Fake.Core
open Fake.DotNet
open Fake.DotNet.Testing
open Fake.Testing
open Expecto

let runCreateProcess setParams =
let _, cp =
"projectfile.csproj"
|> SpecFlowNext.createProcess (fun param ->
{ setParams param with ToolPath = "specflow" })

let file, args =
match cp.Command with
| RawCommand(file, args) -> file, args
| _ -> failwithf "expected RawCommand"
|> ArgumentHelper.checkIfMono

let expectedPath = Path.Combine("specflow", "specflow.exe")
Expect.equal file expectedPath "Expected specflow.exe"

expectedPath, cp.Command.CommandLine

[<Tests>]
let tests =
testList "Fake.DotNet.Testing.SpecFlow.Tests" [
testCase "Test that new argument generation works" <| fun _ ->
let p, cp =
SpecFlowNext.createProcess (fun param ->
{ param with
ToolPath = "specflow"
SubCommand = SpecFlowNext.MsTestExecutionReport
})

let file, args =
match cp.Command with
| RawCommand(file, args) -> file, args
| _ -> failwithf "expected RawCommand"
//|> ArgumentHelper.checkIfMono
let expectedPath =
Path.Combine("specflow", "specflow.exe")
Expect.equal file expectedPath "Expected specflow.exe"
Expect.equal cp.Command.CommandLine
(sprintf "%s MsTestExecutionReport" expectedPath) "expected proper command line"
]
let expectedPath, commandLine =
runCreateProcess (fun param ->
{ param with
SubCommand = SpecFlowNext.MsTestExecutionReport })

Expect.equal commandLine
(sprintf "%s MsTestExecutionReport --ProjectFile projectfile.csproj" expectedPath) "expected proper command line"

testCase "Test that argument generation fails with exception if project file is not given" <| fun _ ->
Expect.throws
(fun _ -> SpecFlowNext.createProcess "" id |> ignore)
"expected to throw an exception"

testCase "Test that argument generation works with default arguments" <| fun _ ->
let expectedPath, commandLine =
runCreateProcess id

Expect.equal commandLine
(sprintf "%s GenerateAll --ProjectFile projectfile.csproj" expectedPath) "expected proper command line"

testCase "Test that argument generation works with all arguments set" <| fun _ ->
let expectedPath, commandLine =
runCreateProcess (fun param ->
{ param with
SubCommand = SpecFlowNext.NUnitExecutionReport
BinFolder = Some "bin/debug"
OutputFile = Some "output.html"
XmlTestResultFile = Some "testresult.xml"
TestOutputFile = Some "testoutput.txt"
FeatureLanguage = Some "de-DE"
Verbose = true
ForceRegeneration = true
XsltFile = Some "transform.xsl" })

Expect.equal commandLine
(sprintf "%s NUnitExecutionReport --ProjectFile projectfile.csproj --binFolder bin/debug --OutputFile output.html --xmlTestResult testresult.xml --testOutput testoutput.txt --FeatureLanguage de-DE --verbose --force --XsltFile transform.xsl" expectedPath ) "expected proper command line"
]

0 comments on commit b9e4a32

Please sign in to comment.