0
how can I add test cases in my code
8 Antworten
+ 4
Based on what I see in your Sololearn profile, it looks like you're just beginning to program in Python, Kotlin and some websites so I'd recommend just adding more code.
Say you had a function called sum in JavaScript. You could run the following code:
if (3 !== sum(1, 2)) {
console.error('3 expected but sum of 1 and 2 found to be ' + sum(1, 2));
}
I would put automated tests in an isolated function or file to clarify that it is purely for testing.
Since you're really new to programming, I don't recommend using a testing framework like other answers suggest just because it seems more complicated than you're prepared for and you didn't specify a language. If this is for client-side JavaScript for example, a client-side testing framework like PhantomJS with Jasmine is pretty tricky to configure. If you want UI tests, that too can be tricky to set up.
In more professional software development, the testing code should be separated so it is compiled and run only in development environments and not in production installation or a live website that has real customers using it. This is because performance is often sacrificed to make problems easier to trace and test in development.
There are a lot of automated testing frameworks that are very helpful for large professional projects. There are frameworks for unit testing, testing client-side JavaScript, testing API's, UI testing, and frameworks for different programming languages.
+ 3
Josh Greig I'm not sure if you were referring to me as one of the responses you disagreed with.
I don't believe I actually made any recommendations one way or the other. 😉
That said, I'm assuming anyone asking this question may have recently stumbled upon the concept of unit testing and wants to dig deeper.
To that end, configuring unit tests are quite boilerplate these days and should be a preferred option for learning sooner than later. This is especially true for JS.
Note, I'm referring specifically to unit testing in isolation, which is different from functional end-to-end, aka integration, testing.
End to end testing would require more setup using a headless server like PhantomJS or Selenium Web Drivers for web UI interactions.
However, using mocha or jest testing libraries with chai / sinon / etc for web frontend have made life much easier.
I agree that many pros do struggle with testing as a practice. They probably also struggle efficiently maintaining code quality as well.
(continued...)
+ 2
Martin Taylor Well... I learned something new today.
That Wikipedia page blew my mind. 🤯
In all my years of writing unit tests across multiple languages and platforms, I've never been aware that anyone, anywhere used the term "XUnit" to generically refer to SUnit / JUnit / NUit / RUnit / etc.
We have only referred to the respective unit testing libraries by name or generically referred to them as "unit tests".
This is probably for the following reasons:
1. Not all unit testing libraries follow the [X]Unit naming convention.
2. On teams that primarily work with one language, it was more natural to refer to the actual library by name.
3. On teams that worked with multiple languages with unit testing frameworks, it was clearer to be explicit about the library by name.
4. Biggest reason of all... xUnit is actually the name of a very popular unit test library in .NET.
Has the term "XUnit" been the common expression to generically refer to unit test libraries in your circles?
+ 2
Josh Greig It's better to start sooner than later with unit testing. Integration testing can follow later.
In the meantime, I'm sharing a few links here that show examples of ad hoc Unit Testing I've done in SoloLearn as an alternative to a testing framework.
I hope sharing these links are helpful. 😉👌
List of Various ad hoc Unit Tests for Different Languages
https://code.sololearn.com/cnnrMYDDmP2x/?ref=app
https://code.sololearn.com/W6kxbpp9Q0YN/?ref=app
https://code.sololearn.com/c9VnW585YuDL/?ref=app
https://code.sololearn.com/WTXM65UyH70G/?ref=app
https://code.sololearn.com/cqviyFWL1rYo/?ref=app
https://code.sololearn.com/c6C913OMYnNF/?ref=app
https://code.sololearn.com/cZ4yQR79fIkY/?ref=app
0
I don't agree with other answers here recommending a testing framework purely because Harsh looks very new to programming and it could be very difficult for him to install and set up a testing framework.
I know people with years of experience professionally developing software who don't make it a habit to create automated testing and definitely leave out certain types of tests due to the maintenance difficulties with things like UI tests or client-side JavaScript unit tests.