diff --git a/tests/README.md b/tests/README.md
index ed54b0b822..748784c776 100644
--- a/tests/README.md
+++ b/tests/README.md
@@ -22,6 +22,12 @@ To run the tests run:
 IPA_SERVER_HOST=<ipaserver_host_or_ip> pytest
 ```
 
+To use a specific domain or realm set `IPA_SERVER_DOMAIN` (defaults to 'test.local') or `IPA_SERVER_REALM` (defaults to uppercase `IPA_SERVER_DOMAIN`):
+
+```
+IPA_SERVER_DOMAIN=<ipaserver_domain> IPA_SERVER_REALM=<ipaserver_realm> pytest
+```
+
 If you need to run using a different user you can use `ANSIBLE_REMOTE_USER`
 environment variable. For example:
 
@@ -36,7 +42,6 @@ environment variable. For example:
 IPA_SSH_PASSWORD=<ipaserver_ssh_password> IPA_SERVER_HOST=<ipaserver_host_or_ip> pytest
 ```
 
-
 To run a single test use the full path with the following format:
 
 ```
@@ -61,7 +66,7 @@ To see why tests were skipped use `-rs`. For example:
 IPA_SERVER_HOST=<ipaserver_host_or_ip> pytest -rs
 ```
 
-For a complete list of options check `pytest --help`.
+For a complete list of `pytest` options check `pytest --help`.
 
 ### Disabling and enabling playbook tests
 
diff --git a/tests/utils.py b/tests/utils.py
index 9e3d5e83be..5892cc389b 100644
--- a/tests/utils.py
+++ b/tests/utils.py
@@ -20,6 +20,7 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 import os
+import socket
 import pytest
 import re
 import subprocess
@@ -32,6 +33,18 @@
 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
 
 
+def is_ip_addr(ipaddr):
+    """Test if given IPA_SERVER_HOST is an IP address."""
+    try:
+        socket.inet_pton(socket.AF_INET, ipaddr)
+    except socket.error:
+        try:
+            socket.inet_pton(socket.AF_INET6, ipaddr)
+        except socket.error:
+            return False
+    return True
+
+
 def get_docker_env():
     docker_env = os.getenv("RUN_TESTS_IN_DOCKER", None)
     if docker_env in ["1", "True", "true", "yes", True]:
@@ -88,8 +101,17 @@ def get_enabled_test(group_name, test_name):
 def get_inventory_content():
     """Create the content of an inventory file for a test run."""
     ipa_server_host = get_server_host()
-
     container_engine = get_docker_env()
+
+    if (
+        ipa_server_host
+        and container_engine is None
+        and not is_ip_addr(ipa_server_host)
+    ):
+        default_domain = ipa_server_host.split(".", 1)[-1]
+    else:
+        default_domain = "test.local"
+
     if container_engine is not None:
         ipa_server_host += f" ansible_connection={container_engine}"
 
@@ -97,12 +119,18 @@ def get_inventory_content():
     if sshpass:
         ipa_server_host += " ansible_ssh_pass=%s" % sshpass
 
+    ipaserver_domain = os.environ.get("IPA_SERVER_DOMAIN", default_domain)
+    ipaserver_realm = os.environ.get(
+        "IPA_SERVER_REALM",
+        ipaserver_domain.upper()
+    )
+
     lines = [
         "[ipaserver]",
         ipa_server_host,
         "[ipaserver:vars]",
-        "ipaserver_domain=test.local",
-        "ipaserver_realm=TEST.LOCAL",
+        "ipaserver_domain=%s" % ipaserver_domain,
+        "ipaserver_realm=%s" % ipaserver_realm,
     ]
     return "\n".join(lines).encode("utf8")