-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
7154-Improve-testdir-documentation-on-makefiles #7239
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
7154-Improve-testdir-documentation-on-makefiles #7239
Conversation
…ting the same file can occur
forgot py35 support, will address |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for taking the time to contribute this @symonk!
The updated docs are definitely a plus, I'm a bit curious about the use case for Testdir.created_files
though. Can you give some examples in tests where this can be useful?
src/_pytest/pytester.py
Outdated
testdir.makepyfile("foobar") | ||
# to create multiple files, pass kwargs accordingly | ||
testdir.makepyfile(custom="foobar") | ||
# outcome is test_something.py & custom.py |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# outcome is test_something.py & custom.py | |
# at this point, both 'test_something.py' & 'custom.py' exist in the test directory |
changelog/7154.improvement.rst
Outdated
@@ -0,0 +1 @@ | |||
Testdir now keeps track of created file paths, updated Testdir documentation to reflect overwritten files |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Testdir now keeps track of created file paths, updated Testdir documentation to reflect overwritten files | |
``Testdir.created_files`` returns a ``Set`` of paths created by ``make*file`` family of methods. |
But I'm curious, what's the use case for this new property?
src/_pytest/pytester.py
Outdated
return ret | ||
|
||
@property | ||
def created_files(self) -> Set: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def created_files(self) -> Set: | |
def created_files(self) -> Set[py.path.local]: |
src/_pytest/pytester.py
Outdated
@@ -650,8 +652,17 @@ def to_text(s): | |||
p.write(source.strip().encode(encoding), "wb") | |||
if ret is None: | |||
ret = p | |||
if ret.strpath not in self._created_files: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest to replace both lines by:
self._created_files.add(ret)
No need to check for membership, and I think keeping a set of py.path.local
is better. 👍
src/_pytest/pytester.py
Outdated
testdir.maketxtfile("foobar") | ||
# to create multiple files, pass kwargs accordingly | ||
testdir.maketxtfile(custom="foobar") | ||
# outcome is test_something.txt & custom.txt |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# outcome is test_something.txt & custom.txt | |
# at this point, both 'test_something.txt' & 'custom.txt' exist in the test directory |
src/_pytest/pytester.py
Outdated
"""Shortcut for .makefile() with a .py extension.""" | ||
r"""Shortcut for .makefile() with a .py extension | ||
defaults to the test name .py e.g test_foobar.py | ||
Calling without kwargs will result in overwriting the initial file contents |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can remove this phrase, because it conveys the impression that by calling with kwargs initial file contents are somehow not overwritten.
src/_pytest/pytester.py
Outdated
"""Shortcut for .makefile() with a .txt extension.""" | ||
r"""Shortcut for .makefile() with a .txt extension | ||
defaults to the test name .txt e.g test_foobar.txt | ||
Calling without kwargs will result in overwriting the initial file contents |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can remove this phrase, because it conveys the impression that by calling with kwargs initial file contents are somehow not overwritten.
testing/test_pytester.py
Outdated
def test_testdir_duplicate_paths(testdir): | ||
testdir.makepyfile("foo") | ||
testdir.makepyfile("bar") | ||
assert len(testdir.created_files) == 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert len(testdir.created_files) == 1 | |
assert testdir.created_files == {testdir.tmpdir.join("test_testdir_duplicate_paths.py")} |
This is better because we are more explicit about the outcome, helping to figure out any problems in case the test fails.
testing/test_pytester.py
Outdated
def test_files_by_name_are_correct(testdir): | ||
testdir.maketxtfile("foo") | ||
testdir.maketxtfile(custom="foobar") | ||
assert len(testdir.created_files) == 2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert len(testdir.created_files) == 2 | |
assert testdir.created_files == {testdir.tmpdir.join("test_files_by_name_are_correct.py"), testdir.tmpdir.join("custom.py")} |
Ditto.
testing/test_pytester.py
Outdated
two = testdir.maketxtfile(data) | ||
three = testdir.makeconftest(data) | ||
four = testdir.makeini(data) | ||
assert {one, two, three, four} == testdir.created_files |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert {one, two, three, four} == testdir.created_files | |
assert testdir.created_files == {one, two, three, four} |
For consistency with the rest of the tests: "obtained == expected"
@nicoddemus thanks for the fast feedback, appreciate it - hope you are well. The new property i guess i jumped the gun a bit there, was doing some plugin development and was creating a lot of files as part of my testing, found it easier to use that rather than keep going to the temp directory and inspecting them on FS - updated accordingly the documentation and plucked out the work that isnt really beneficial. Thanks again |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot @symonk, we appreciate it!
Good afternoon, hope you are in good health in the current climate. I have made some minor adjustments to TestDir and some of its accompanying documentation, thanks for your time and look forward to receiving some feedback. Helps with #7154
Added the ability to Testdir to track created files and allow tests to access this data, includes test coverage for this feature.
Added some documentation around makepyfile/maketxtfile to explain that when a name is not passed, the files contents will be overwritten in place
Include documentation when adding new features.
Include new tests or update existing tests when applicable.
Allow maintainers to push and squash when merging my commits. Please uncheck this if you prefer to squash the commits yourself.