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.

5
  • 1
    what not disable your connection from the control panel? or even enable Airplane mode Commented Aug 3, 2016 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. Commented Aug 3, 2016 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. Commented Aug 3, 2016 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). Commented Aug 3, 2016 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. Commented Aug 3, 2016 at 19:15

3 Answers 3

3

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."

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

3

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%.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.