3

I understand NUnit runs tests in parallel, which isn't what this question is about exactly. In a single test, I can have two local socket connections open using TcpClient on different ports, which can communicate with one another in the same unit test. The problem is the address space is of course shared by the thread executing the unit test, so it doesn't simulate communication through a network how I want.

If I have a test called Test1 with one TcpClient, and whilst it's running, I run Test2 with the other TcpClient, in these two tests I can have both the unit tests talking to one another and communicating - testing the protocol I've designed with separate address spaces, which does give me the simulation of a network I desire, and allows me to do the proper test assertions.

Now the question is, is there any way to do this automatically? I'd like to have a single test that can run Test1 & Test2, where both these tests communicate via their respective sockets, instead of starting them both up manually in parallel.

user4779
  • 645
  • 5
  • 14
  • Why do you need to separate address spaces? Are there any static variables involved? – PMF Nov 18 '22 at 10:48
  • @PMF There are, some globals. But I guess on a technical level it's not necessary, but I'd just like to have everything run as separate processes so I don't accidentally rely on any shared data in memory - the tests are quite extensive – user4779 Nov 18 '22 at 11:15

2 Answers2

3

NUnit is not designed to run tests in different processes (nor is any other unit testing framework I'm aware of). It's designed to start as many tests as possible in the same process, to increase performance (creating processes is expensive).

Of course, that doesn't mean it's impossible to do this, but it's not a domain of NUnit itself. The question doesn't give much information about how the tests are structure, but I'm assuming there's a test assembly for the server and another test assembly for the client. Only when they run at the same time, the tests work.

I would go ahead and create a third test assembly that just starts the other two, really creating processes manually, e.g. launching

dotnet test ServerUnitTests.dll
dotnet test ClientUnitTests.dll

["Integration Tests" would be the better term here, but that's a detail]

PMF
  • 14,535
  • 3
  • 23
  • 49
  • Hi @PMF Thank you for this answer. What would you recommend to conduct the sort of integration test in my question? A normal non-test project with tests like that, with assertions done by throwing exceptions? Or would you recommend I still use NUnit with the different assemblies compiled as you've described? Just curious what's viewed as best practice. Thanks – user4779 Nov 21 '22 at 06:04
  • I'm not so much used to client/server applications, so I'm probably the wrong guy to ask for best practices. But I would do something like the above. What confuses me, though: Are you really running different tests on the server side, too? For such a scenario, I would expect to "normally" start the server and then run client side tests on it. You can't run tests that start and stop the server (e.g. with different configurations) while the client tests run. – PMF Nov 21 '22 at 06:20
  • It's all happening locally both the sockets connections. In essence I'm simulating the server on the same machine just on a different port – user4779 Nov 21 '22 at 08:11
  • 1
    Ok, that makes sense, but they you probably don't want to start the server with `dotnet test` but just with `dotnet run myserver.dll `. – PMF Nov 21 '22 at 08:23
0

I do not think that is possible.you can decide which test should run after another, so basically order. but usually, tests are not required to be run in parallel.

Ankit Jasuja
  • 15
  • 1
  • 5