New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unittest does not run properly #100
Comments
@foxpowa In your in your test cases you need to pass the subcommand that you need to run by default it would be import unittest
from unittest.mock import MagicMock
from scripts.my_script import MyScript, ClassA, ClassB
class MyScriptTests(unittest.TestCase):
_fake_A_classes = [ClassA('ped1'), ClassA('ped2')]
_fake_B_class = ClassB('ped1','A','A', False, False)
def test_my_script_my_function(self):
my_script = MyScript(["run"]).start() # changed here
my_script.A_classes = \
MagicMock(
return_value=self._fake_A_classes
)
returned_ped = my_script.my_function(self._fake_B_class)
self.assertEqual('ped1', returned_ped.name) if there is a necessary for passing any other command line arguments you need to take the string and split it using shlex import shlex
arguments = shlex.split("run --arg_one /root/file")
# Now the arguments variabel should be given to your class intialization
# MyScript(arguments).start() |
Hi @jaswanth098, I've tested your snippet like this : import unittest
from unittest.mock import MagicMock
import shlex
from scripts.my_script import MyScript, ClassA, ClassB
class MyScriptTests(unittest.TestCase):
_fake_A_classes = [ClassA('ped1'), ClassA('ped2')]
_fake_B_class = ClassB('ped1','A','A', False, False)
def get_script_arg(self, args_string: str): # <---------------------------------
return shlex.split(args_string)
def test_my_script_my_function(self):
args = self.get_script_arg("run my_file.txt") # <---------------------------------
my_script = MyScript(args).start() # <---------------------------------
my_script.A_classes = \
MagicMock(
return_value=self._fake_A_classes
)
returned_ped = my_script.my_function(self._fake_B_class)
self.assertEqual('ped1', returned_ped.name) and experienced this issue : python -m unittest discover -s tests -p "*_tests.py"
...
Traceback (most recent call last):
File "/path/to/repo/tests/my_script_tests.py", line 18, in test_my_script_my_function
my_script = MyScript(args).start()
TypeError: __init__() takes 1 positional argument but 2 were given Which seems legit to me, as Best regards, |
@foxpowa, In that case, you can try something like below class MyScript:
def __init__(self, *args, **kwargs):
# custom code
super().__init__(*args, **kwargs) Even custom arguments can be passed to your class class MyScript:
def __init__(self, custom_args, *args, **kwargs):
# custom code
# you can do anything with custom_args but donot pass them to super
super().__init__(*args, **kwargs) BaseScript init function has to |
@foxpowa And one more thing in the above code after your import unittest
from unittest.mock import MagicMock
import shlex
from scripts.my_script import MyScript, ClassA, ClassB
class MyScriptTests(unittest.TestCase):
_fake_A_classes = [ClassA('ped1'), ClassA('ped2')]
_fake_B_class = ClassB('ped1','A','A', False, False)
def get_script_arg(self, args_string: str):
return shlex.split(args_string)
def test_my_script_my_function(self):
args = self.get_script_arg("run my_file.txt")
my_script = MyScript(args) # <---------------------------------
my_script.start() # <---------------------------------
my_script.A_classes = \
MagicMock(
return_value=self._fake_A_classes
)
returned_ped = my_script.my_function(self._fake_B_class)
self.assertEqual('ped1', returned_ped.name) |
Thank you @jaswanth098 Your advice about class MyScript:
def __init__(self, *args, **kwargs):
# custom code
super().__init__(*args, **kwargs) worked ;) For unit test purposes, I don't even need to exec Thank you again ! |
Hi there,
First, thank you for your package ;)
I'm unfortunately running on an issue. I've written unit tests, and trying to run them, I'm getting this error :
Here is a sample from my unittest file :
and from my script :
It's not my real Classes, path, functions name.
I'm wondering how to run unittest for a basescript script ?
Any help would be appreciated.
Best regards
The text was updated successfully, but these errors were encountered: