#unit-testing
1 messages Β· Page 30 of 1
I don't use the API to run tests; I just use the command in the terminal. I'm not familiar with the test runner API.
That function seems to run a specific suite
I do know there is unittest.main(). I believe it will run all tests in the current module.
I got it, thanks
I'm using pytest and having some issues. I want to use an async fixture with parameterize for a test.
This is what my decos look like.
@pytest.mark.asyncio
@pytest.mark.parametrize(
"map_name", "creator_id", "map_type", "difficulty", "mechanics",
[ ...
and this is my test signature
async def test_get_maps(
async_pg_connection: database.Database, map_name, creator_id, map_type, difficulty,
mechanics
):
...
I'm getting this type error:
TypeError: Metafunc.parametrize() takes from 3 to 6 positional arguments but 7 positional arguments (and 1 keyword-only argument) were given
Any guidance on where I am going wrong?
--
Nevermind, I was using parameterize wrong!
Hello everyone,
Do you have an idea for how can I generate a reference (it's like an ID) and it has to be different every time I run my code
I think you can use uuid4 for that: https://docs.python.org/3/library/uuid.html#example. Whenever you call it, it will generate a new unique id
Ok thank you
And if I want this id to have a specific structure , I think it's impossible no ?
What structure do you need?
Your unit tests should be deterministic
I meant by that generate reference id according to the format : trigram + date+ a number and this have to be unique for every customer I add in my app (since I am working on a customer application)
use your database auto incrementing primary key
But this channel is about unit testing
Sorry
Ok thanks
i have a question about mocking - i've noticed that when i import a function i.e. from moduleA import functionA, the function does not get properly mocked, but when i do import moduleA and call the mocked function with moduleA.functionA, the mock works correctly. Does anyone have any resources or a page in the mock docs or a quick explanation on why that is? if it matters, i'm using pytest-mock but it says it uses the same API as unittest.mock
The docs have a section dedicated to this https://docs.python.org/3/library/unittest.mock.html#where-to-patch
thank you very much!
most QA Test Automation jobs in my are require you to know JAVA, why not Python - i don't understand
There is a lot of java in the enterprises. So it's natural to use it as it simplifies a few things and enables sharing a lot of libraries and practices and infrastructure.
That said, I also did see companies using java for their product and python for the test automation and end to end tests
How do you all deal with logging during testing? My program logs some stuff, but it becomes quite noisy during tests. Should I just disable the logging except for the test cases that actually test the logs?
I actually set the verbosity higher for my tests as it makes it easier to pinpoint the problem and wouldn't care about it as long as they pass
That also helps in the context of CI since I can just look a the logs from the get go
Sometimes the logs are helpful. Other times they emit useless warnings that aren't relevant in the context of a test (or are warnings for scenarios I am deliberately creating for a test)
If I have a bunch of useless log messages then it makes it more difficult to separate the signal from the noise
i.e. test results from useless logs
Maybe it's a symptom of the test runner not nicely presenting the information
If the logs are useless, then when are they useful?
Also it sounds like you are referring more about end to end / integration tests than unit tests?
For example, there is a warning that the configuration is potentially insecure. And maybe some users want that, so it's not a fatal error. Anyway, for the tests, I am not concerned with that warning since it's up to the user whether they want that security.
And yes, it's more like an integration/e2e test
yeah, if I was in your shoes, I would either tweak the logging levels for the e2e tests, or make sure logs are separated between the tests so it's easier to find. But overall, I would rather make the logging better than purely remove it as it would make understand failures more difficult and take more steps
I could lower the level to "info" or create a separate level for "warning but not in tests"
It feels more urgent than just "info" though
And creating a separate level is kinda hacky
Right now I've been setting the minimum level for these specific tests to "error" for the modules from which the warnings are emitted. So logging isn't totally disabled.
pytest by default hides logging during tests run
we can enable it back with -s flag, when we wish to run specific test π€
controlling test verbosity, and having different stuff for DEBUG / ERROR / WARNING should help i guess π€
but in the end the problem could be in that everything in logging is global? perhaps we could set sub modules of logging, with setting different logging levels in them
dunno... I don't really rely on logging during testing.
I really on logging only to make some visibility during program run
although you know, it could be useful i guess to verify that necessary logging is present during some espectially important operations perhaps
then I will be sure it will be present during prod run
hello can anyone tell me how can i write unit test cases for protobuff using server and client
Create the server in a ThreadPoolExecutor and then contact it from the client
How does one test a class module that has no arguments in any of its functions except for "self". For example how would you test a function that changes status between the string 'False' and 'True'?
What's a class module?
it looks like we need to make an instance of the class, and using its preferably public methods to write tests.
Preferably connections to the class from other code would be done by including
from other_code import BlaBla
class Thing:
connection_to_other_code = BlaBla()
if needed I would have replaced those inclusions with mock methods. If I could use the class without mocks, I would have used it that way. Unless I would encounter some test performance thing
Although you know, nothing forbids me to use private methods of the class in its private tests π€ but still using only public methods has some better taste
I have the same question whereas I find having testing done in Python using pytest is way better and simpler than java and test ng
Probably that's just because they use Java Spring Backend
backend should remain in one language if possible
tests are part of backend code
less effort/cost to have devs maintaining one code type instead of two
Hey all,
I need some help with guidance of best practice pytest usege, a way to implement something:
I have fixture that doing setup / teardown.
This fixture used in function scope for specific test.
In this test I need to add another integration of Jira, that will create new execution and parse the result which will add the test ID to the execution.
Which way is better to do so?
Way 1:
Create another parametrized fixture with autouse=True and module scope, when it will actually used at the end of the test to parse the results
Way 2:
Create module utils file that will serve the interaction of the test by calling the util function with the Jira staff
Any other ideas?
Hi I am using python flask and getting an error in pytest and no idea how to fix it. Can someone help?
Here is the github https://github.com/NML240/flaskblog2
I changed one line, the new line is below.
response = make_app_run_in_test_env.get("/verified_email<token>")
This line also causes the error. Let me add the error message.
I am getting an error when I type python -m pytest in python flask. The error is ValueError: unknown url type.
Here is the error. https://pastebin.com/HJJXUznW
Any further questions just ask
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
is anybody familiar with using @parameterized.class()?
i
Hello All,
I am trying to scrape system requirements from top 5 steam games. I've been able to achieve everything except picking up the values for parameters like OS, platform, graphics etc. I am using following xpath to pick up the text:
//div[@class='game_area_sys_req_leftCol']//ul[@class='bb_ul']//strong[contains(text(),'OS')]//following-sibling::text()[1]
Error:
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: The result of the xpath expression "//div[@class='game_area_sys_req_leftCol']//ul[@class='bb_ul']//strong[contains(text(),'OS')]//following-sibling::text()[1]" is: [object Text]. It should be an element.
I need to submit this test tomorrow in my class. Please help
When checked manually, we can find the value to be picked up with the same xpath
Is there any way that I can verify the data sent by an aiohttp session POST request?
Iβm using aioresponses as of now to mock out aiohttp requests. But one can just send status code and payload data for a given URL and Method, I canβt seem to find how one can verify the data sent
I would really appreciate it if anyone can help me with this.
Can you tell me in brief what is happening here?
Like are you using any property in aioresponses to handle it?
def latest_request(mocked, request_type, path):
return mocked.requests.get((request_type, URL(path)))
def json_of_latest_request(r):
return r[-1].kwargs["json"]
with aioresponses() as mocked:
mocked.post( "https://example.com/webhooks/actions", payload={"events": [], "responses": []}, )
await remote_action.run( default_channel, default_nlg, default_tracker, default_domain )
r = latest_request(mocked, "post", "https://example.com/webhooks/actions")
assert r
assert json_of_latest_request(r) == ...
Oh, thanks a lot!
code
!eval <code, ...>
Can also use: e
*Run Python code and get the results.
This command supports multiple lines of code, including code wrapped inside a formatted code block. Code can be re-evaluated by editing the original message within 10 seconds and clicking the reaction that subsequently appears.
If multiple codeblocks are in a message, all of them will be joined and evaluated, ignoring the text outside of them.
We've done our best to make this sandboxed, but do let us know if you manage to find an issue with it!*
Hey, I am a Java programmer and I have recently picked up Python mostly for web development and API development, although I have not developed any APIs or services before I have been following the footsteps of my predecessors at work and I read and understand code quite well. One of the things I want to learn is to write unit tests for some of these code written in Flask.
One of the things I have been doing to test is by running "flask run" and then hitting localhost with the path. I'm sure there are better ways of doing it. I read about pytest and another similar sounding unit test framework and I want pointers for a place to start. Looking forward to learning more python and become a better programmer
Sure. Firstly i would recommend official Flask docs, they are nice, they recommend how to install pytest
https://github.com/darklab8/darklab_backend_roadmap
Also recommending books mentioned in my roadmap in testing section https://raw.githubusercontent.com/darklab8/darklab_backend_roadmap/master/swe_backend.drawio.svg
Unit testing by vladimir khorkiov, and TDD by kent beck should give enough background to go
also Python has TDD book with goat picture. It describes stuff with pytest... but i am a bit hesitant to recommend it. I know for sure that Knorikov and Kent Beck are reallly good. Not sure that sure about Python TDD book
Software written with tests and without tests is havint 3 times difference in quality xD
Software written with tests first/or keeping them at least in mind while mentally thinking about architecture, and having written software later is having 9 times difference
so are most python users using unittest or pytest? what is the difference?
use pytest.
python has doctest for completely minimal case
then unittest, which is way better, but it is just inbuilt library without super comfort
then goes pytest, which supports everything that unittest does, but much more at the same time
result: having nice and DRY code
I have python code which is for web application.i need to write unit test cases.Need help...I'm new to coding
Trying to create a virtual environment but having trouble
You should get out of the python repl
I have the following directory structure:
scripts/
- test.py
- main.py
subdir/
__init__.py
foo.py
bar.py
In foo.py, there is a line from bar import baz. Now I want to set up some tests.
So in test.py I can do from subdir.bar import baz and test baz.
But when I do from subdir.foo import main then it gives this error:
File "<redacted>\test.py", line 4, in <module>
from subdir import foo
File "<redacted>\subdir\foo.py", line 1, in <module>
from bar import baz
ModuleNotFoundError: No module named 'bar'
I kinda understand where the error comes from, but I'm not sure about best practices on how to solve this. Can someone help me?
To answer my own question: test.py or the tests folder should be in the same folder as from where the main script is called from the command line (e.g. main.py). Scripts in subdir that import from other scripts in subdir cannot be called from the command line because they use lines like from subdir.bar import baz which is only possible if the namespace is in the parent directory, where main.py resides.
Guess this was more of a question about imports than about testing.
$ tree app/tests
app/tests
βββ __init__.py
βββ test_forms.py
βββ test_models.py
βββ test_views.py
an accomplished 10 seconds of work. Those empty files should remind me to write tests inside of them when the pre-commit hooks don't want to commit empty files. π
image 1
Top left, shows very simple tests/tests_tests.py
Top right; its demonstrated that these pass without isses loading the settings file
Then the error when I try doing tests/test_models.py
that is shown in image2 top left at the very top
...
from django.conf import settings
settings.configure()
django.setup()
User = get_user_model()
that's all it is. The lines about configure and setup are just me trying to work around it.
You can see where ive shown values of pytest.ini and various env ars trying make it work.
What is the hangup with the settings file that it seems to easily misplace.
and then passes in another file with simpler tests?
the error
$ pytest ../tests/test_models.py
===================================== test session starts ======================================
platform linux -- Python 3.9.5, pytest-7.1.2, pluggy-1.0.0
rootdir: /home/steel/projects/slackmanic
collected 0 items / 1 error
============================================ ERRORS ============================================
____________________________ ERROR collecting tests/test_models.py _____________________________
venv-linux/lib/python3.9/site-packages/django/apps/registry.py:158: in get_app_config
return self.app_configs[app_label]
E KeyError: 'auth'
During handling of the above exception, another exception occurred:
venv-linux/lib/python3.9/site-packages/django/contrib/auth/__init__.py:170: in get_user_model
return django_apps.get_model(settings.AUTH_USER_MODEL, require_ready=False)
venv-linux/lib/python3.9/site-packages/django/apps/registry.py:208: in get_model
app_config = self.get_app_config(app_label)
venv-linux/lib/python3.9/site-packages/django/apps/registry.py:165: in get_app_config
raise LookupError(message)
E LookupError: No installed app with label 'auth'.
During handling of the above exception, another exception occurred:
../tests/test_models.py:11: in <module>
User = get_user_model()
venv-linux/lib/python3.9/site-packages/django/contrib/auth/__init__.py:176: in get_user_model
raise ImproperlyConfigured(
E django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'auth.User' that has not been installed
=================================== short test summary info ====================================
ERROR ../tests/test_models.py - django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL ...
!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!
I am just trying to load the custom user model, but it has trouble finding the settings a lot
which are specified
even exported directly into the environment before running, that didnt help either
When using mocked_cls = mock.create_autospec(MyClass) I've noticed that many unit tests don't seem to have any issues with mocked_cls.some_method() when the argument instance=True was left out.
Am I not understanding how that should behave or is mock just very forgiving in this case?
it seems like you don't have auth defined in your INSTALLED_APPS
are you using a separate settings.py for tests in your django project?
no, its just not finding the settings file which is configured. Since i posted this, i found this article, which seems to be aware of the problem i have https://engineroom.trackmaven.com/blog/a-better-development-environment-with-docker-and-fig/
iam trying what they do.
wait
wrong link
that's weird, it should be able to find it regardless
so what the problem has to do with is... accessing a value from the settings conf before it is initialized
this is what happens when you extend the auth user model
because you have to reference is from a reference point
its confusing but i get it
what you have to do is...
treat setting up the tests like your manage.py file and initialize things
before you can access the values from the settings
i gather this would happen automaatically if you didnt have to reference the user model the way you do when you extend it
well
lets say thaat i hope this is what the problem is and it makes sense to me now as im working on it
how do i start unit testing?
Start with reading to understand what it is
Then join the cult of TDD
(Technically optional, but really nice to know and sometimes to go TDD only, writing tests first)
(Sometimes we write first and then cover with tests later(within same day at least)
And just read features of https://docs.pytest.org/en/7.1.x/ to get all python related things
Well... u would also need some inbuilt features of unittest library to get mocking and patching in addition
That would be... from low to average medium understanding of testing.
Then u need also to learn OOP/Design Patterns/Clean Architecture/Refactorization(Martin Fowler) stuff in order to make conscious effort how to refactorize your work after you wrote tests, or just having conscious understanding how to make architecture better testable. Then u practice all of it, and u will reach eventually senior level of understanding about how to write testable software of a high quality
P.S. Building architecture through Composition is in general nice to have testable architecture.
ok
to be honest, the thing that is helping me finally make sense of how to write tests is...
its not the test driven approach people tell you which is confusing and feels alien and backwards. When you're new you don't know what to test. You don't understand why things will become broken without tests even if they seem to work without them. That takes time of working on projects that break a lot.
What i recommend is finding a project like...
a django web application for example and instead of thinking that tests are going to drive your development at first, find a project that has thorough testing and write ups about why it needs it.
eventually the TDD will be more natural
but its not at first.
also aa good plaace to start is when you spend time trying to find a fix for something and you finally get it... write a test that makes that fix you just found asserted.
that is aa likely thing to break again if you dont
those are not expert tips but the they are newbie tips to testing that help get you actually staarted doing it
Another thing to think about is, say you have "found the solution to a problem"
What if the desired behavior is a coincidence and a side effect of something else actually?
how will you know?
when it breaks again and you try to fix in a waay that is wrong?
you'll know when your tests fail
tldr, it can be useful to write tests "after development" too and not just for "driving it"