Hello, I'm trying to implement multiple test cases...
# pact-net
c
Hello, I'm trying to implement multiple test cases for a pact.net consumer unit test. Whenever I increase the number of test cases, for example from one test case to two test cases or from two to three the pact.VerfiyAsync() method of one of the test cases throws the following exception: System.InvalidOperationException : Unable to start mock server If I reduce again the number of test case, for example by commenting out one of the test case methods, everything works again. I have created an example project based on the pact.net basic .net example with some files from my project added to that and was able to reproduce the behavior: https://github.com/cyrusdevnomad/test-project-for-error-reproducing You can try it for yourself. But make sure to change the number of unit test cases (the exception is thrown if the number of unit test cases is increased) by uncommenting/commenting out unit test case methods to see the exception mentioned above.
y
if it works with one, but fails with two, are you using random ports? is it unable to start the mock server, because it's already started on that port
c
Locally it has always worked with one, but often failed with two or three methods. But in the pipeline its failing even with one method. But I have not checked in pipeline whether the port is really free or not. I can try to do that. But in case of multiple unit test methods, would it be a good choice to have each unit test method using a different free port?
y
mock servers are stateful, so if you have async tests and you use the same port in tests you will run into issues. using the same port you would not want to run your tests in parallel, which means slower tests random port per test means you spin up a mock server per test which is isolated, you need pact file write mode set to merge otherwise the last mock server to write to fill will clobber. pipeline sounds like a separate issue if it doesn't work at all
c
Thanks to your inputs I was able to resolve the problem. I have indeed different unit test methods that each call an async method of the tested client. Now I have changed my code to spin up the mock server per test and it seems to work now even without setting the write mode to merge. But because of the point you metioned I will try to set the write mode as you suggested. Now its working in pipeline too.
m
FYI I don’t think on the most recent version of Pact .NET that option exists - it was something unique to the Ruby core (which version of Pact .NET are you running? If 4+ then it should automatically merge)
you should let the OS pick a dynamic port
c
I'm using Pact.NET Version="4.4.0". So that would explain why my configuration seems to work even without having configured any merge option. The port was hard coded to 9000 in that version that I put on GitHub. That version was meant to reproduce the problem I was facing. But I have another local version that is working now and for this version, I spin up a mock server for each test and each time I look programmatically for a port that is available. I guess that would match your suggestion to let the OS pick a dynamic port yes?
💯 1
m
yep!