8

I have a component that (by part) uses an internet connection. I wrote some UnitTests to ensure that to component is working. However, I would like to test the behaviour of the component without internet connections.

So, my goal is to somehow temporary disable internet, or the whole internet connection, and reactivate after test.

Nate
  • 30,286
  • 23
  • 113
  • 184
curator
  • 175
  • 2
  • 10
  • 1
    what not disable your connection from the control panel? or even enable Airplane mode – user1234433222 Aug 03 '16 at 19:08
  • 1
    Split the tests into two suites: one to ensure your internet code is correct, and the other to check that the code handling the data is correct. The second suite should never *need* to touch the internet: all test cases should be specified in code. – RoadieRich Aug 03 '16 at 19:13
  • 3
    Testing requires a hardware abstraction layer. Instead of asking the OS whether internet is available, the component should ask the abstraction layer. Then the test mocks the abstraction layer with a controlled answer instead of asking the OS. – Ben Voigt Aug 03 '16 at 19:13
  • 1
    For exhaustive testing, you need to be able to mock all the I/O code -- otherwise you have a race condition trying to cause a failure at a particular I/O call (whether that be network or disk or some other interface). – Ben Voigt Aug 03 '16 at 19:15
  • There are lots of good suggestions here. If modifying the test isn't an option, then just disable wireless, unplug your Ethernet cable, or power down the router. That makes it kind of a pain to test though. – DVK Aug 03 '16 at 19:15

3 Answers3

3

I would disable\enable like here local are connection in test initialization

[ClassInitialize]
SelectQuery wmiQuery = new SelectQuery("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionId != NULL");
ManagementObjectSearcher searchProcedure = new ManagementObjectSearcher(wmiQuery);
foreach (ManagementObject item in searchProcedure.Get())
{
    if (((string)item["NetConnectionId"]) == "Local Network Connection")
    {
       item.InvokeMethod("Disable", null);
    }
}

 [ClassCleanup()]
 // Enable local area connetcion
Community
  • 1
  • 1
Shachaf.Gortler
  • 5,655
  • 14
  • 43
  • 71
2

There are many ways in which the system could have "No Internet" and the answer really depends on what you mean.

As the accepted other answer suggests, you could simply disable the network interface. That guarantees you have no internet, but the computer also will know it has no network either.

A couple other options are

  1. To remove your Default Gateway (this may require setting static IP settings in the control panel, though I'm sure you could do it programmatically as well)

This way, the computer still thinks it's connected, but it won't have any network access except on the local subnet

  1. Remove DNS server settings, see above link.

This way, the computer has direct IP based access but to a regular user it would appear as if there was "no internet."

Nate
  • 30,286
  • 23
  • 113
  • 184
2

Whilst not a direct answer to your question I believe you may find some use in this tool - https://jagt.github.io/clumsy/download

I've used it at work to simulate different network conditions for an mobile app that I'm currently working on. It is possible to completely disable the network connection by setting packet drop to 100%.

Tom Biddulph
  • 514
  • 6
  • 16