How to structure unit tests [Answered]

Tthinker2278/24/2022
This is more of a preference question than anything but how do you usually structure tests? I'm... not very good at writing tests and I haven't done it a lot, so I don't have that much experience. Do you typically create a separate test class for every type you want to test with methods within to tests specific functionalities of the class?
ZZacharyPatten8/24/2022
entirely depends on the context
ZZacharyPatten8/24/2022
just try to have meaningful tests
ZZacharyPatten8/24/2022
having 100% test coverage is not the goal
ZZacharyPatten8/24/2022
the goal is to have meaningful tests that will notify you in specific situations
HHicho8/24/2022
if you unit test methods in ThatService.cs I'd create ThatServiceTests.cs
HHicho8/24/2022
so, yeh
HHicho8/24/2022
you can also follow/mimic your project structure in your tests - Data.csproj/Data.Tests.csproj then classes to follow whichever class you test
HHicho8/24/2022
in short yes on your last question 🙂
OOrannis8/24/2022
There are many testing philosophies. I'm a strong proponent of: if you can avoid unit tests and write functional tests instead, do so
OOrannis8/24/2022
Not every codebase can do this, of course
HHicho8/24/2022
what is a functional test? like end to end ones?
OOrannis8/24/2022
Mostly.
OOrannis8/24/2022
For example, in the compiler, we don't test individual lowering passes
HHicho8/24/2022
yeah, that feels like best way to test stuff
OOrannis8/24/2022
We test the resulting IL
OOrannis8/24/2022
And we test observable results from our public APIs
HHicho8/24/2022
unit test on the other hand give quick feedback when you're code gets built by the pipeline
HHicho8/24/2022
your*
OOrannis8/24/2022
Right, that's one reason to use unit tests if you need to
OOrannis8/24/2022
For Roslyn, functional tests are more than fast enough for that feedback loop
HHicho8/24/2022
do you use 3rd party libs for this? like specflow?
OOrannis8/24/2022
No
OOrannis8/24/2022
Not beyond Xunit, anyway
Tthinker2278/24/2022
Hmm, alright
Tthinker2278/24/2022
I think I'll like group things loosely by what they do then
OOrannis8/24/2022
If you're specifically talking about nanopass, Thinker, I'd suggest functional tests 🙂
Tthinker2278/24/2022
How would that work?
Tthinker2278/24/2022
From what you described it sounds more like "try it and see and debug if it doesn't work as expected" over testing specific parts of the app.
MMMayor McCheese8/24/2022
I thought you were an nunit guy
MMMayor McCheese8/24/2022
I’ve used specflow at times, but you definitely need to justify the need imho
Tthinker2278/24/2022
I meant unit tests and not any kind of other tests
HHicho8/24/2022
so, our friend 333fred, suggested functional tests are better than unit test so we kind of diverged, with regards to unit test what you assumed in your original question (last question specifically) is the way to go imho.
OOrannis8/24/2022
I am
OOrannis8/24/2022
But the compiler uses x
OOrannis8/24/2022
Yep
OOrannis8/24/2022
Use your public API to create input, and then test that the output is what you expect
JJayy8/24/2022
it def depends on your general app, integration tests are a vital part of a webapi
JJayy8/24/2022
but a compiler doesnt necessarily care about them
JJayy8/24/2022
we run a full suite of tests against a live db on every workflow run
Tthinker2278/24/2022
How do you run tests against a live DB?
Tthinker2278/24/2022
Anyway I think I'll go with unit tests combined with some functional tests
MMMayor McCheese8/24/2022
I'll use a test-but-actual db, it gets spun up for the test
Tthinker2278/24/2022
sounds expensive
MMMayor McCheese8/24/2022
What is the cost of a bug in production?
MMMayor McCheese8/24/2022
like a non trivial show stopper
Tthinker2278/24/2022
I've never been in production so idk, but probably a lot
MMMayor McCheese8/24/2022
I tell my teams that a basic medium quality production bug costs the company about 5k
MMMayor McCheese8/24/2022
that was some back of the napkin math
MMMayor McCheese8/24/2022
but spinning up a cloud database, inserting some data, running some tests, and destroying the cloud database is only a few minutes
Tthinker2278/24/2022
Minutes? wow
MMMayor McCheese8/24/2022
the most extreme case I've seen is 20 minutes
MMMayor McCheese8/24/2022
we have scripted data for tests, so deploy the database resource, deploy the scripts, run the tests, verify the tests, etc.
JJayy8/24/2022
We connect to a cloud database from our runner
Tthinker2278/24/2022
You have a custom test runner?
JJayy8/24/2022
Yee i wrote a custom fixture
JJayy8/24/2022
It xunit tho
JJayy8/24/2022
So it just just a custom xunit fxiture
JJayy8/24/2022
Every test gets a totally new database
JJayy8/24/2022
Well, sorta, the data is cleared and reseeded per test
MMMayor McCheese8/24/2022
I keep the data for a test run
JJayy8/24/2022
Even if the test inserts data?
MMMayor McCheese8/24/2022
it all gets wiped at the end
JJayy8/24/2022
Ohh ya
JJayy8/24/2022
We wipe them reinsert
AAccord8/24/2022
✅ This post has been marked as answered!