Fable python unit tests, like Fable.Mocha with Expecto syntax.
$ dotnet add package Fable.Pyxpecto| Latest | Prerelease | Downloads |
|---|---|---|
This repository is heavily inspired by Fable.Mocha by the awesome @Zaid-Ajaj.
Inspired by the popular Expecto library for F# and adopts the testList, testCase and testCaseAsync primitives for defining tests.
Fable.Pyxpecto can be used to run tests in Python, JavaScript, TypeScript and .NET! Or use compiler statements to switch between Pyxpecto and keep using Fable.Mocha and Expecto!
Table of Contents
/// Reuse unit tests from Expecto and Fable.Mocha
let tests_basic = testList "Basic" [
testCase "testCase works with numbers" <| fun () ->
Expect.equal (1 + 1) 2 "Should be equal"
testCase "isFalse works" <| fun () ->
Expect.isFalse (1 = 2) "Should be equal"
testCase "areEqual with msg" <| fun _ ->
Expect.equal 2 2 "They are the same"
testCase "isOk works correctly" <| fun _ ->
let actual = Ok true
Expect.isOk actual "Should be Ok"
]
Pending tests will not be run, but displayed as "skipped".
ptestCase "skipping this one" <| fun _ ->
failwith "Shouldn't be running this test"
ptestCaseAsync "skipping this one async" <|
async {
failwith "Shouldn't be running this test"
}
If there are any focused tests all other tests will not be run and are displayed as "skipped".
👀 Passing the
--fail-on-focused-testscommand line argument will make the runner fail if focused tests exist. This is used to avoid passing CI chains, when accidently pushing focused tests.Example
py my_focused_tests_file.py --fail-on-focused-testswill fail.
let focusedTestsCases =
testList "Focused" [
ftestCase "Focused sync test" <| fun _ ->
Expect.equal (1 + 1) 2 "Should be equal"
ftestCaseAsync "Focused async test" <|
async {
Expect.equal (1 + 1) 2 "Should be equal"
}
]
Actually all tests run with this library will be sequential. The function is only added to comply with Expecto syntax.
💬 Help wanted. I currently have a prototype implementation for parallel tests on a branch. But it breaks collecting run-tests in .NET.
Running any py/ts/js/net code from pyxpecto can be customized with flags:
Fable.Pyxtpecto (F#)
Author: Kevin Frey
Usage:
(python/node/npx ts-node/dotnet run) <path_to_entrypoint> [options]
Options:
--fail-on-focused-tests Will exit with ExitCode 4 if run with this argument
and focused tests are found.
--silent Only start and result print. No print for each test.
--do-not-exit-with-code Will only return integer as result and not explicitly call `Environment.Exit`.
This can be useful to call Pyxpecto tests from foreign test frameworks
These can also be given via:
[<EntryPoint>]
let main argv =
!!Pyxpecto.runTests [|
ConfigArg.FailOnFocused
ConfigArg.Silent
|] all
From Nuget with:
paket add Fable.Pyxpecto<PackageReference Include="Fable.Pyxpecto" Version="0.0.0" />Fable.Pyxpecto does not use any dependencies and tries to support as many fable languages as possible. Check out the multitarget test project to see it fully set up!
open Fable.Pyxpecto
// This is possibly the most magic used to make this work.
// Js and ts cannot use `Async.RunSynchronously`, instead they use `Async.StartAsPromise`.
// Here we need the transpiler not to worry about the output type.
#if !FABLE_COMPILER_JAVASCRIPT && !FABLE_COMPILER_TYPESCRIPT
let (!!) (any: 'a) = any
#endif
#if FABLE_COMPILER_JAVASCRIPT || FABLE_COMPILER_TYPESCRIPT
open Fable.Core.JsInterop
#endif
[<EntryPoint>]
let main argv = !!Pyxpecto.runTests [||] all
Then run it using:
dotnet rundotnet fable {rootPath} -o {rootPath}/{js_folder_name}node {rootPath}/{js_folder_name}/Main.js"type": "module".npm init.dotnet fable {rootPath} --lang ts -o {rootPath}/{ts_folder_name}npx ts-node {rootPath}/{ts_folder_name}/Main.tsdotnet fable {rootPath} --lang py -o {rootPath}/{py_folder_name}python {rootPath}/{py_folder_name}/main.pypython (v>=3.14) executable on your PATH, or replace python with path/to/python.exe.
Might work with versions as low as 3.12
installed fable-library python dependency (version = "5.0.0a17")
Use the following syntax to automatically switch between Expecto, Fable.Mocha and Pyxpecto:
#if FABLE_COMPILER_PYTHON
open Fable.Pyxpecto
#endif
#if FABLE_COMPILER_JAVASCRIPT
open Fable.Mocha
#endif
#if !FABLE_COMPILER
open Expecto
#endif
[<EntryPoint>]
let main argv =
#if FABLE_COMPILER_PYTHON
Pyxpecto.runTests [||] all
#endif
#if FABLE_COMPILER_JAVASCRIPT
Mocha.runTests all
#endif
#if !FABLE_COMPILER
Tests.runTestsWithCLIArgs [] [||] all
#endif
⚠️ If you want to use Pyxpecto in combination with Fable.Mocha you need to conditionally set Fable.Mocha dependency as shown below. Without this fable will try to transpile Fable.Mocha to python, which will result in errors.
<!-- .fsproj file-->
<PackageReference Condition="'$(FABLE_COMPILER_JAVASCRIPT)' == 'true'" Include="Fable.Mocha" Version="2.17.0" />
👀 Everything in curly braces are placeholders
dotnet fable {path/to/tests} --lang py -o {path/to/tests}/pypython {path/to/tests}/{EntryFileName.py}uv --version (Tested with 0.9.13)dotnet --version (Tested with 7.0.306)node --version (Tested with v22)npm --version (Tested with 11.6)Run all commands in root.
dotnet tool restorenpm install./build.cmd runtests
Can be specified to run tests for specific environment.
Switch test project
./build.cmd runtestsdotnet./build.cmd runtestsjs./build.cmd runtestspyMultitarget test project
./build.cmd runmtpy./build.cmd runmtjs./build.cmd runmtts./build.cmd runmtnet./build.cmd runtestsdotnet pack ./src/Fable.Pyxpecto -o ./pkg./pkg to Nuget