How do I write integration tests
Here is my scenario:
User interacts with UI (Avalonia) -> user inputs information, lets say its checkboxes in a checklist -> user clicks I.E submit button -> command on view model takes all the data the user inputted on the UI and sends it to an api parsing service -> api parsing service formats all the data into the expected API objects and sends it to the writing service -> writing service sends it off to the api.
What I want to automatically validate is that the API response in every command is successful. Our services are broken out into interfaces like IApiParsingClient, IApiWriteClient, and I don't have an issue hard coding test data somewhere to do this. I need to learn how and where to start
79 Replies
change the title to integration tests :p
How do I change the title :kekw:
open a #help thread
this is one silly
I don't get what you're trying to test here. viewmodel or API?
if there's no test project yet, create a new XUnit project to start
@Teddy So yeah what I wanna write tests for is simulating, which I can hardcode, user data from the UI to the relay commands that process
The commands I wanna focus on right now interact with a client class that talks to our API
I want to expect successful results back from the API
I would like to start at VM level which calls the api client bc some of our VM code modifies data to fit the api expectations
But if that gets messy I can go straight to api client testing to start out with
Okay I have created a new xunit project under my solution
jump down to here https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-9.0#basic-tests-with-the-default-webapplicationfactory you'll use the WebApplicationFactory to spin up an instance of your API
you know we have xunit projects already right
No
you do now
😂 that was one of the first things I asked
Where
in the tests solution folder
They dont have tests in them if we do
Oh thats been hidden for months since we dont interact with them lol
Insider information :kappa:
There's a project for API, services, and UI tests
I never said I had tests
I said test projects
lol
We don't spin the API up so I can skip that
API is 24/7 by India
it'll run a lot faster if you run it locally
I made tests and all are passing.
Production ready
My hands r tied
never have a unit test reference a non-local API 😄
By Pune, India
i mean you could, it'll just take forever and debugging will be a nightmare
I want to have 1k unit tests that run in a second
We'll have to do that
adding an API call to that is fucking hell
I don't really think we have a choice, right @Ainz ?
the database is kind of the problem here
I have a mirror of the API source code, but without a local database and all of the stored procedures that go with it, the API is kind of useless
All of the business logic is stored procedures
We will just accept slow testing
We r used to slow
The API acts more as a gateway / forwarder to a SP call at the DB
tsqlt testing, then
The real question is: what are you testing with your tests if the api itself is useless :HUHH:
I guess it's more of a 3rd party regression testing
Anyway, one day a wise man said: testing is doubting.
We are different teams
They push updates independently of us
We want to automatically test all of our API calls every time they update
To make sure we are still in sync with them
anyway @Mango you can start by adding a reference to the client/UI in your xunit project, then make use of its client to make API calls and assert on the responses
Sometimes they change fields without telling us
Then everything is broken and its impossible to debug bc they don't give back normal error messages
And its a huge PITA to figure out
Hours of guesswork :sadge:
We'd like to catch it as soon as possible
Learn to coordinate.
Head up that initiative.
yeah, so really you're regression testing someone else's API
Get promoted over
A
but really they should be the ones doing it
yeah. you give them something they can run against first.
I don't think @Ainz would mind one bit if I got promoted to a position to support them
In fact he'd probably laugh at me and say nope dont even bother me with it
Nobody wants that responsibility
other than this
Surprise, we shipped!bullshit
We have plans pushing them to improve things surely but slowly
The guys we have on it right now are good people. Just needed direction which Ainz is setting them up with
I'm hopeful for the future anyways
Teddy
anyway <@386969677143736330> you can start by adding a reference to the client/UI in your xunit project, then make use of its client to make API calls and assert on the responses
Quoted by
<@405963936987611146> from #How do I write integration tests (click here)
React with ❌ to remove this embed.
not that I think it's the best approach, but it's somewhere to start
make sure the API you're calling isn't connected to a prod DB
Why not best approach?
Heh its connected to prod db but we have an account that is for test results on the prod db
because like me and @Pope said, you're adding a lot of latency to the tests, so if you want to automate them (include in your CI/CD) it'll slow things down substantially, you also can't just set breakpoints in the API to help debug since it's running somewhere remotely
Yeah it kinda sucks
But it is what it is
Be the improvement we want to see. Believe in yourself.
Rewrite all the SP from scratch and host my own database
I'll have it done by tonight thanks Pope I feel inspired
this is a good example of outsourcing gone too far though and the negative consequences from it
I still think it could be made to work
they need to take some ownership of the code back from the offshore
just need a bit of collaboration on expected changes. Communicate what changes are being made, have them write a test harness or something
i see it being pretty painful, though. nm.
it's just going to turn into waterfail development
Timezones are the biggest issue
We have an actual corporate in India so they aren't "offshore" in traditional sense. They have their own contracted normal hours to work
They are full employees not an offshore contract
So we cannot force them to be on our timezone and vice versa
then more lockstep dev
@Zane so I reference my api project
This means I have to re-implement my IWriteClient in the test project right?
No way to bring my implementation over from the other project?
That would be pretty magical
https://nsubstitute.github.io/
ah, yes, this answers the previous question I somehow missed the ping on
you can bring in your implementations whenever applicable though
@Zaneris This shit is so involved
But I think I understand now
it can be yeah, depending on how complex your service chain is
I thought testing was plug and play not gonna lie
Complex AF
I have another problem
We have a method with an out parameter. I need that method mocked
The return is useless to me
I somehow need to define that out in nsubstitute not the return
Can I do that
Aha got it
Thats actually nice syntax
see, you're a pro already
A huge part of it is that its just so boring
And tedious
this is why tests often get delegated to the interns/juniors
but then they need help and the senior ends up doing all of the work anyway 😄
Me in a call with @Ainz rn LMAO
But he is busy so we r just rubber ducky rn
And chatting
he needs better taste in music
it comes through his mic
apple music recommendations are garbage
So I have, on phone so ima shorthand stuff
Gotta do this like 3x more to get to the usable client
Or get up to it I should say
I believe you can get it up 🎉
at least once it's done you can just write actual tests from there forward
So true
Question
So when I go to make my usable client which requires all these deps
I have to make an actual instance of it right I can't use substitute and somehow insert these other mocks
Internet looks like its telling me I can make an actual instance now but just sanity checking
2 options, register the mocked services through DI and then the new client will just grab the mocked ones automatically, or just instantiate the client with you providing the mocked versions yourself
I usually just instantiate it myself providing all of the mocks
Doing ILogger is gonna be weird
Why do you need to?
Just assume it's perfect and ignore it.
My client requires it and tries to use it
Do I not have to mock it?
Im currently mocking factory 2/10 wooo
Which requires me to setup like another 5 dependencies
Pass in an ILogger that does nothing.
NullLogger<T>