Introduction
For this week in open source development, we were asked to implement testing for our static site generators. We had to research for the optimum testing platform for our code base. Since my static site generator uses Python, I opted to use pytest.
The reason I chose pytest is because it is one of the most popular Python testing platforms and allows for very in depth test creations. I also really liked that Visual Studio Code could utilize it, which made my implementations that much easier.
Implementing Testing Code
Since my static site generator has many helper functions that allow me to process folders, files, write to files, create css files, etc, I began my quest of writing my tests. At first, I had some difficulty understanding how pytest works, but after a few sample test cases, I began getting the hang of it.
Since my code works with files, my approach was to call the functions, let them produce the expected file, and verify by using assert
command. The assert command allows us to verify the expectation of the return of a function. However, since my functions are mainly void, I would assert that a file was created, such as this:
assert os.path.isfile("example.html")
To view all of my tests, click on this commit 4b18e4b
Conclusion
Overall, I had a lot of fun implementing testing code via pytest, and it really challenged me. I believe that with the addition of new functions, I will be able to quickly create effective tests that will ensure my code is up to the full standards that I want to achieve.
One thing that I noticed was the difference in how my code checks for the environment. This is something that I need to relook into because since I am developing on a Windows computer and not a Linux based, the paths of the files are different, which introduced a small headache whenever I was dealing with paths.
Top comments (0)