Skip to content

Commit d3ae8a3

Browse files
committed
Bugfix: don't try to instantiate abstract classes with tests.
1 parent d425153 commit d3ae8a3

File tree

3 files changed

+49
-34
lines changed

3 files changed

+49
-34
lines changed

Fuchu.Tests/MbUnitTestTypes.fs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,9 @@ module MbUnitTestTypes =
8686
[
8787
TestCase("test 1", fun () -> ())
8888
TestCase("test 2", fun () -> ())
89-
]
89+
]
90+
91+
[<AbstractClass>]
92+
type AbstractWithTest() =
93+
[<Test>]
94+
member x.ATest() = ()

Fuchu.Tests/MbUnitTests.fs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ module MbUnitTests =
8383
TestLabel("DivTest(1000,10,100)", TestCase _),
8484
TestLabel("DivTest(4195835,3145729,1.3338196)", TestCase _)))))))) -> ()
8585
| _ -> failtestf "unexpected %A" test
86+
87+
testCase "abstract class" <| fun _ ->
88+
let testType = typeof<AbstractWithTest>
89+
let test = MbUnitTestToFuchu testType
90+
match test with
91+
| TestList (Seq.One (TestList Seq.Empty)) -> ()
92+
| _ -> failtestf "unexpected %A" test
8693
]
8794

8895
type MbUnitTestsFromFuchu() =

Fuchu/xUnitHelpers.fs

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -59,39 +59,42 @@ module XunitHelpers =
5959
Invoke = fun (o: obj) -> invoke o m })
6060

6161
let TestToFuchu (attr: TestAttributes) (testCategory: Type -> string) (testType: Type) (testMethods: TestMethod seq) =
62-
let methods = nonIgnoredMethods attr.Ignore testType
63-
let methodsWithAttrs = methodsWithAttrs methods
64-
let setupMethods = methodsWithAttrs [attr.Setup]
65-
let teardownMethods = methodsWithAttrs [attr.TearDown]
66-
let fixtureSetupMethods = methodsWithAttrs [attr.FixtureSetup]
62+
if testType.IsAbstract
63+
then TestList []
64+
else
65+
let methods = nonIgnoredMethods attr.Ignore testType
66+
let methodsWithAttrs = methodsWithAttrs methods
67+
let setupMethods = methodsWithAttrs [attr.Setup]
68+
let teardownMethods = methodsWithAttrs [attr.TearDown]
69+
let fixtureSetupMethods = methodsWithAttrs [attr.FixtureSetup]
6770

68-
TestList <| seq {
69-
if Seq.length testMethods > 0 then
70-
yield testList (testType.FullName + testCategory testType) <| seq {
71-
let testInstance = create testType
72-
let inline invoke metod = invoke testInstance metod
73-
Seq.iter invoke fixtureSetupMethods
74-
for tm in testMethods ->
75-
test tm.Name {
76-
try
77-
Seq.iter invoke setupMethods
78-
try
79-
tm.Invoke testInstance
80-
match tm.ExpectedException with
81-
| Some expectedExc ->
82-
failtestf "Expected exception '%s' but no exception was thrown" expectedExc
83-
| None -> ()
84-
with
85-
| :? TargetInvocationException as e ->
86-
match tm.ExpectedException with
87-
| Some expectedExc ->
88-
let innerExc = e.InnerException.GetType().AssemblyQualifiedName
89-
if expectedExc <> innerExc
90-
then failtestf "Expected exception '%s', got '%s'" expectedExc innerExc
91-
| None ->
92-
raise e.InnerException
93-
finally
94-
Seq.iter invoke teardownMethods
71+
TestList <| seq {
72+
if Seq.length testMethods > 0 then
73+
yield testList (testType.FullName + testCategory testType) <| seq {
74+
let testInstance = create testType
75+
let inline invoke metod = invoke testInstance metod
76+
Seq.iter invoke fixtureSetupMethods
77+
for tm in testMethods ->
78+
test tm.Name {
79+
try
80+
Seq.iter invoke setupMethods
81+
try
82+
tm.Invoke testInstance
83+
match tm.ExpectedException with
84+
| Some expectedExc ->
85+
failtestf "Expected exception '%s' but no exception was thrown" expectedExc
86+
| None -> ()
87+
with
88+
| :? TargetInvocationException as e ->
89+
match tm.ExpectedException with
90+
| Some expectedExc ->
91+
let innerExc = e.InnerException.GetType().AssemblyQualifiedName
92+
if expectedExc <> innerExc
93+
then failtestf "Expected exception '%s', got '%s'" expectedExc innerExc
94+
| None ->
95+
raise e.InnerException
96+
finally
97+
Seq.iter invoke teardownMethods
98+
}
9599
}
96-
}
97100
}

0 commit comments

Comments
 (0)