24 points · by cobrakailab · 5y ago · 18 comments
So I came up with this lightweight library to help enforce unit tests with a Given-When-Then structure. I hope you find this useful. Any feedback are welcome.
https://github.com/cobrakai-lab/Cobrakai.GWTUnit
[Fact(Skip = "Generated With Snippet")]
public async Task MyTestMethodAsync()
{
//Arrange
//Act
//Assert
throw new NotImplementedException();
}
If that results in hard to read unittests I suspect that your library would too, as it is a matter discipline. If we assume that we use your terms, "Given, When, Then" in place of "Arrange, Act, Assert" - can you give an argument that would favour your solution with the syntactic overhead of putting it all in lambdas?I also prefer AAA tests, but I can't stand it when people leave those comments in as some kind of region markers. IMO, they're useful when teaching AAA tests, but it seems, in my experience, most developers actually have those comments in every. single. test. That's some hardcore cargo culting.
I just use a blank line to separate the different stages, and if you have problems making that readable, you probably need to refactor the Arrange or Assert parts to use some helper methods.
I realize I'm probably overreacting, but I feel the anger rising whenever I see this in the wild (and have to suppress it).
I generally also advise the leads to make sure there is a reasonable limit to the length of a test method. As you say if it's too long it probably needs some work before it's allowed in the main branch.
I of course mean leaving the comments in the final test. I don't care about the comments in the actual snippet.
Vs "arrange a csv with a missing column act read it assert the exception contains the missing column name"
> If we assume that we use your terms, "Given, When, Then" in place of "Arrange, Act, Assert"
Besides, it's been a while since I did any C# programming, but does it really "enforce" the structure, or only encourage it?
I mean, what's to stop people from going eg.
int ingredient1 = 1;
int ingredient2 = 2;
Given(() =>
{
Func<int, int, int> noodleMaker = (x, y) => x + y;
// ...
Putting some declarations outside of the "given" section?If it's just a matter of good will and adhering to the convention, then we're kind of back to square one - if you're a good scout, you could have stuck to the convention even without the extra library.
Unless there's something I'm missing
[TestMethod]
public void ShouldBeAbleToTestActions()
{
var dwarfs = new List<string>() { "Sneezy", "Bashful", "Sleepy", "Happy", "Grumpy", "Doc", "Dopey" };
Action<List<string>> DoDarkMagic = (creatures) => { creatures.RemoveAt(0); }
DoDarkMagic(dwarfs)
Should("have less dwarfs", () => { dwarfs.Count.Should().Be(6); });
Should("missing Sneezy", () => { dwarfs.Should().NotContain("Sneezy"); });
}; Action<List<string>> DoDarkMagic = (creatures) => { creatures.RemoveAt(0); }
DoDarkMagic(dwarfs)
There is no point in putting the action in a variable, only to invoke it on the next line. This is a meandering way of expressing: // Do Dark Magic
dwarfs.RemoveAt(0);
Extract a private method if you're keen on keeping the name.What benefit do these abstractions give me over simply coding an Arrange/Act/Assert?
Tests in larger c# projects are often a mess.
People then turn to BDD or frameworks like to this to try to impose order.
Sometimes they produce better tests the second time around, but more often the additional overhead of the framework just means that "now you have 2 problems".
Probably no benefit, IMHO. Work on your tests, but avoid "high concept" frameworks.
But when your system is a bit more complex or a legacy, your unit tests are complex, your team is not that mature, not everyone follows good practice, comments does not match with reality or even crunch multiple scenarios into a single tests, asking them to re-write unit test may sounds rude...(guess I might sounds more like complaining now...), but anyway, that is kinda how all of a sudden this idea came to me the other day while I was writing my own unit tests.
My ideas is if my team can adopt this, at least this can help us improve unit test qualities and readability. Then maybe once we become a more mature team, we can ditch the 'crutches'.
One point to note from your introduction was the following statement: I often find unit tests are hard to read, and especially harder to quickly identify what are the important pieces, or even what the test is testing about
Have you considered pairing with others in your team or using group code reviews to see if there is a style or understanding gap? This may be far more effective than trying to get people to use a novel unit testing framework with little or no real longevity.
I'm designing an internal library myself that does the same thing using Kotlin's EDSL support (you can specify the "this" type of a function) and splits test logic up into required, act, and provided blocks. E.g.
required {
// will be made true for test
check(something)
}
act {
// the act under test
click(someButton)
}
provided {
// will be verified for test
check(something)
}
The interesting thing here is that the polarity of the required and provided blocks can be flipped (check the required is true, and if it is make the provided true) to do fakes/mocks. Well, it is still something I'm working on.