Repository: avocado-framework/avocado

Issue:
Python-unittest: Avocado do not "nrun" tests in the current directory
Running the python-unittest from the `unit` directory with `avocado run` works as expected:

```
[wrampazz@wrampazz unit]$ pwd

/home/wrampazz/src/avocado/avocado.dev/selftests/unit
[wrampazz@wrampazz unit]$ avocado run test_plugin_assets.py
/home/wrampazz/src/avocado/avocado.dev/avocado/plugins/run.py:296: FutureWarning: The following arguments will be changed to boolean soon: sysinfo, output-check, failfast and keep-tmp. 
  warnings.warn("The following arguments will be changed to boolean soon: "
JOB ID     : 4b4bfb3b65595e7450036ff4265e85550663d819
JOB LOG    : /home/wrampazz/avocado/job-results/job-2020-05-22T09.47-4b4bfb3/job.log
 (01/10) test_plugin_assets.AssetsPlugin.test_fetch_assets_sucess_fail: PASS (0.22 s)
 (02/10) test_plugin_assets.AssetsPlugin.test_fetch_assets_sucess: PASS (0.22 s)
 (03/10) test_plugin_assets.AssetsPlugin.test_fetch_assets_fail: PASS (0.22 s)
 (04/10) test_plugin_assets.AssetsPlugin.test_fetch_assets_empty_calls: PASS (0.22 s)
 (05/10) test_plugin_assets.AssetsClass.test_visit_classdef_valid_class: PASS (0.23 s)
 (06/10) test_plugin_assets.AssetsClass.test_visit_classdef_invalid_class: PASS (0.22 s)
 (07/10) test_plugin_assets.AssetsClass.test_visit_fuctiondef_valid_class: PASS (0.22 s)
 (08/10) test_plugin_assets.AssetsClass.test_visit_fuctiondef_invalid_class: PASS (0.22 s)
 (09/10) test_plugin_assets.AssetsClass.test_visit_assign_valid_class_method: PASS (0.23 s)
 (10/10) test_plugin_assets.AssetsClass.test_visit_assign_invalid_class_method: PASS (0.23 s)
RESULTS    : PASS 10 | ERROR 0 | FAIL 0 | SKIP 0 | WARN 0 | INTERRUPT 0 | CANCEL 0
JOB HTML   : /home/wrampazz/avocado/job-results/job-2020-05-22T09.47-4b4bfb3/results.html
JOB TIME   : 2.72 s
```

Running the same python-unittest from the `unit` directory with `avocado nrun` does not work as expected:

```
[wrampazz@wrampazz unit]$ avocado nrun test_plugin_assets.py
Status server started at: 127.0.0.1:8888
3-test_plugin_assets.AssetsPlugin.test_fetch_assets_fail spawned and alive
5-test_plugin_assets.AssetsClass.test_visit_classdef_valid_class spawned and alive
6-test_plugin_assets.AssetsClass.test_visit_classdef_invalid_class spawned and alive
10-test_plugin_assets.AssetsClass.test_visit_assign_invalid_class_method spawned and alive
8-test_plugin_assets.AssetsClass.test_visit_fuctiondef_invalid_class spawned and alive
2-test_plugin_assets.AssetsPlugin.test_fetch_assets_sucess spawned and alive
9-test_plugin_assets.AssetsClass.test_visit_assign_valid_class_method spawned and alive
7-test_plugin_assets.AssetsClass.test_visit_fuctiondef_valid_class spawned and alive
4-test_plugin_assets.AssetsPlugin.test_fetch_assets_empty_calls spawned and alive
1-test_plugin_assets.AssetsPlugin.test_fetch_assets_sucess_fail spawned and alive
Finished spawning tasks
Status server: exiting due to all tasks finished
Tasks result summary: {'error': 10}
Tasks ended with 'error': 6-test_plugin_assets.AssetsClass.test_visit_classdef_invalid_class, 9-test_plugin_assets.AssetsClass.test_visit_assign_valid_class_method, 5-test_plugin_assets.AssetsClass.test_visit_classdef_valid_class, 2-test_plugin_assets.AssetsPlugin.test_fetch_assets_sucess, 1-test_plugin_assets.AssetsPlugin.test_fetch_assets_sucess_fail, 3-test_plugin_assets.AssetsPlugin.test_fetch_assets_fail, 7-test_plugin_assets.AssetsClass.test_visit_fuctiondef_valid_class, 10-test_plugin_assets.AssetsClass.test_visit_assign_invalid_class_method, 8-test_plugin_assets.AssetsClass.test_visit_fuctiondef_invalid_class, 4-test_plugin_assets.AssetsPlugin.test_fetch_assets_empty_calls
[wrampazz@wrampazz unit]$ 
```

Running with verbose shows `No module named <test_name>` for each test, here is one as an example:

```
Task started: 8-test_plugin_assets.AssetsClass.test_visit_fuctiondef_invalid_class. Outputdir: /var/tmp/.avocado-task-przaad_k
Task complete (error): 8-test_plugin_assets.AssetsClass.test_visit_fuctiondef_invalid_class
Task 8-test_plugin_assets.AssetsClass.test_visit_fuctiondef_invalid_class output:
======================================================================
ERROR: test_plugin_assets (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: test_plugin_assets
Traceback (most recent call last):
  File "/usr/lib64/python3.8/unittest/loader.py", line 154, in loadTestsFromName
    module = __import__(module_name)
ModuleNotFoundError: No module named 'test_plugin_assets'


----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)
```

But running the python-unittest using `avocado nrun` from the Avocado root project directory works:

```
[wrampazz@wrampazz avocado.dev]$ pwd
/home/wrampazz/src/avocado/avocado.dev

[wrampazz@wrampazz avocado.dev]$ avocado nrun selftests/unit/test_plugin_assets.py
Status server started at: 127.0.0.1:8888
3-selftests.unit.test_plugin_assets.AssetsPlugin.test_fetch_assets_fail spawned and alive
5-selftests.unit.test_plugin_assets.AssetsClass.test_visit_classdef_valid_class spawned and alive
4-selftests.unit.test_plugin_assets.AssetsPlugin.test_fetch_assets_empty_calls spawned and alive
7-selftests.unit.test_plugin_assets.AssetsClass.test_visit_fuctiondef_valid_class spawned and alive
2-selftests.unit.test_plugin_assets.AssetsPlugin.test_fetch_assets_sucess spawned and alive
8-selftests.unit.test_plugin_assets.AssetsClass.test_visit_fuctiondef_invalid_class spawned and alive
10-selftests.unit.test_plugin_assets.AssetsClass.test_visit_assign_invalid_class_method spawned and alive
9-selftests.unit.test_plugin_assets.AssetsClass.test_visit_assign_valid_class_method spawned and alive
6-selftests.unit.test_plugin_assets.AssetsClass.test_visit_classdef_invalid_class spawned and alive
1-selftests.unit.test_plugin_assets.AssetsPlugin.test_fetch_assets_sucess_fail spawned and alive
Finished spawning tasks
Status server: exiting due to all tasks finished
Tasks result summary: {'pass': 10}
```

Avocado `nrun` should behave the same way as the `run` command when running python-unittests from different directories.


Discussion:
@willianrampazzo @clebergnu I did some investigation here and brought me back to #3666.

Unless I'm missing something, and afaik, the way that the new runner is spawning tasks with a fresh "env" is the cause here.

#3666 was about PATH, but in this case is the same for $PYTHONPATH. If we export PYTHONPATH to the current dir, we will be able to run with nrunner.

```bash
 $ export PYTHONPATH=.:$PYTHONPATH
```

@clebergnu do you have any other suggestion here?
@willianrampazzo @clebergnu ignore (partially) my comment. I think that I found a solution.

Resolving patch:
diff --git a/avocado/core/nrunner.py b/avocado/core/nrunner.py
--- a/avocado/core/nrunner.py
+++ b/avocado/core/nrunner.py
@@ -468,6 +468,7 @@ def _uri_to_unittest_name(uri):
 
     @staticmethod
     def _run_unittest(uri, queue):
+        sys.path.insert(0, ".")
         stream = io.StringIO()
         unittest_name = PythonUnittestRunner._uri_to_unittest_name(uri)
         suite = unittest.TestLoader().loadTestsFromName(unittest_name)


Test patch: