stdlib test runner: implement setup and teardown commands to unit tests (#8776)

# Description

As in other testing frameworks, the `setup` runs before every test case,
and the `teardown` after that. A context can be created in `setup`,
which will be in the `$in` variable in the test cases, and in the
`teardown`. The `teardown` is called regardless of the test is passed,
skipped, or failed.

For example:

```nushell
use std.nu *

export def setup [] {
    log debug "Setup is running"
    {msg: "This is the context"}
}

export def teardown [] {
    log debug $"Teardown is running. Context: ($in)"
}

export def test_assert_pass [] {
    log debug $"Assert is running. Context: ($in)"
}

export def test_assert_skip [] {
    log debug $"Assert is running. Context: ($in)"
    assert skip
}

export def test_assert_fail_skipped_by_default [] {
    log debug $"Assert is running. Context: ($in)"
    assert false
}
```


![image](https://user-images.githubusercontent.com/282320/230466359-9908cc72-edbd-4150-9aff-d15fe42c0cc7.png)

# After Submitting

I'll update the documentation.

---------

Co-authored-by: Mate Farkas <Mate.Farkas@oneidentity.com>
This commit is contained in:
Máté FARKAS
2023-04-10 22:42:11 +02:00
committed by GitHub
parent 9bb2c8faf5
commit c3678764b4
4 changed files with 111 additions and 62 deletions

View File

@@ -3,8 +3,23 @@ use std "xupdate"
use std "xinsert"
use std "assert equal"
export def setup [] {
{sample_xml: ('
<a>
<b>
<c a="b"></c>
</b>
<c></c>
<d>
<e>z</e>
<e>x</e>
</d>
</a>' | from xml)
}
}
export def test_xml_xaccess [] {
let sample_xml = ('<a><b><c a="b"></c></b><c></c><d><e>z</e><e>x</e></d></a>' | from xml)
let sample_xml = $in.sample_xml
assert equal ($sample_xml | xaccess [a]) [$sample_xml]
assert equal ($sample_xml | xaccess [*]) [$sample_xml]
@@ -14,7 +29,7 @@ export def test_xml_xaccess [] {
}
export def test_xml_xupdate [] {
let sample_xml = ('<a><b><c a="b"></c></b><c></c><d><e>z</e><e>x</e></d></a>' | from xml)
let sample_xml = $in.sample_xml
assert equal ($sample_xml | xupdate [*] {|x| $x | update attributes {i: j}}) ('<a i="j"><b><c a="b"></c></b><c></c><d><e>z</e><e>x</e></d></a>' | from xml)
assert equal ($sample_xml | xupdate [* d e *] {|x| $x | update content 'nushell'}) ('<a><b><c a="b"></c></b><c></c><d><e>nushell</e><e>nushell</e></d></a>' | from xml)
@@ -22,7 +37,7 @@ export def test_xml_xupdate [] {
}
export def test_xml_xinsert [] {
let sample_xml = ('<a><b><c a="b"></c></b><c></c><d><e>z</e><e>x</e></d></a>' | from xml)
let sample_xml = $in.sample_xml
assert equal ($sample_xml | xinsert [a] {tag: b attributes:{} content: []}) ('<a><b><c a="b"></c></b><c></c><d><e>z</e><e>x</e></d><b></b></a>' | from xml)
assert equal ($sample_xml | xinsert [a d *] {tag: null attributes: null content: 'n'} | to xml) '<a><b><c a="b"></c></b><c></c><d><e>zn</e><e>xn</e></d></a>'